<!Doctype html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<meta name="viewport" content="width=device-width, initial-scale=1.0" />
	<title>学习 HTML5 Canvas 这一篇文章就够了 | 菜鸟教程</title>

  <meta name='robots' content='max-image-preview:large' />
<link rel='stylesheet' id='classic-theme-styles-css' href='../0/classic-themes.min.css?ver=6.2' type='text/css' media='all' />
<link rel="canonical" href="../w3cnote/html5-canvas-intro.html" />
<meta name="keywords" content="学习 HTML5 Canvas 这一篇文章就够了">
<meta name="description" content="一、canvas 简介 ​&amp;lt;canvas&amp;gt; 是 HTML5 新增的，一个可以使用脚本(通常为 JavaScript) 在其中绘制图像的 HTML 元素。它可以用来制作照片集或者制作简单(也不是那么简单)的动画，甚至可以进行实时视频处理和渲染。 ​它最初由苹果内部使用自己 MacOS X WebKit 推出，供应用程序使用像仪表盘的构件和 Safari 浏览器使用。后来，有人通过 Gecko 内核的浏览器 (尤..">
		
	<link rel="shortcut icon" href="https://static.runoob.com/images/favicon.ico">
	<link rel="stylesheet" href="../0/style.css?v=1.170" type="text/css" media="all" />	
	<link rel="stylesheet" href="../0/font-awesome.min.css" media="all" />	
  <!--[if gte IE 9]><!-->
  <script src=""></script>
  <!--<![endif]-->
  <!--[if lt IE 9]>
     <script src=""></script>
     <script src=""></script>
  <![endif]-->
  <link rel="apple-touch-icon" href="https://static.runoob.com/images/icon/mobile-icon.png"/>
  <meta name="apple-mobile-web-app-title" content="菜鸟教程">
</head>
<body>

<!--  头部 -->
<div class="container logo-search">

  <div class="col search row-search-mobile">
    <form action="index.php">
      <input class="placeholder" placeholder="搜索……" name="s" autocomplete="off">
      
    </form>
  </div>

  <div class="row">
    <div class="col logo">
      <h1><a href="../">菜鸟教程 -- 学的不仅是技术，更是梦想！</a></h1>
    </div>
        <div class="col right-list"> 
    <button class="btn btn-responsive-nav btn-inverse" data-toggle="collapse" data-target=".nav-main-collapse" id="pull" style=""> <i class="fa fa-navicon"></i> </button>
    </div>
        
    <div class="col search search-desktop last">
      <div class="search-input" >
      <form action="//www.runoob.com/" target="_blank">
        <input class="placeholder" id="s" name="s" placeholder="搜索……"  autocomplete="off" style="height: 44px;">
      </form>
      
      </div>
    </div>
  </div>
</div>



<!-- 导航栏 -->
<div class="container navigation">
    <div class="row">
        <div class="col nav">
            

                        <ul class="pc-nav" id="note-nav">
                <li><a href="../">首页</a></li>
                <li><a href="../w3cnote">笔记首页</a></li>
                <li><a href="../w3cnote/android-tutorial-intro.html" title="Android 基础入门教程">Android</a></li>
                <li><a href="../w3cnote/es6-tutorial.html" title="ES6 教程">ES6 教程</a></li>
                <li><a href="../w3cnote/ten-sorting-algorithm.html" title="排序算法">排序算法</a></li>
                <li><a href="../w3cnote/hadoop-tutorial.html" title="Hadoop 教程">Hadoop</a></li>
                <li><a href="../w3cnote/zookeeper-tutorial.html" title="Zookeeper 教程">Zookeeper</a></li>
                <li><a href="../w3cnote/verilog-tutorial.html" title="Verilog 教程">Verilog</a></li>
                <li><a href="../w3cnote_genre/code" title="编程技术">编程技术</a></li> 
                <li><a href="../w3cnote_genre/coderlife" title="程序员人生">程序员人生</a></li>
                
                <!--<li><a href="javascript:;" class="runoob-pop">登录</a></li>
                
                
                        <li>
                <a style="font-weight:bold;" href="../linux/linux-tutorial.html#yunserver" target="_blank" onclick="_hmt.push(['_trackEvent', 'aliyun', 'click', 'aliyun'])" title="kkb">云服务器</a>
                </li>
                <li><a href="http://gk.link/a/104mQ" target="_blank" style="font-weight: bold;"onclick="_hmt.push(['_trackEvent', '极客时间', 'click', 'jike'])" title="我的圈子">极客时间</a></li>
            
                
                <li><a target="_blank" href="../shoppinglist" rel="nofollow">知识店铺</a></li> 
        -->
            </ul>
                        
              
            <ul class="mobile-nav">
                <li><a href="../w3cnote">首页</a></li>
                <li><a href="../w3cnote_genre/android" target="_blank" title="Android 基础入门教程">Android</a></li>
                <li><a href="../w3cnote/es6-tutorial.html" target="_blank" title="ES6 教程">ES6</a></li>
                <li><a href="../w3cnote_genre/joke" target="_blank" title="程序员笑话">逗乐</a></li>
                
                <a href="javascript:void(0)" class="search-reveal">Search</a> 
            </ul>
            
        </div>
    </div>
</div>


<!--  内容  -->
<div class="container main">
	<div class="row">

		<div class="col middle-column big-middle-column">
	 			<div class="article">
			<div class="article-heading">
				<h2>学习 HTML5 Canvas 这一篇文章就够了</h2>				<h3><em>分类</em> <a href="../w3cnote_genre/code" title="编程技术" >编程技术</a> </h3>
			</div>
			<div class="article-body note-body">
				<div class="article-intro">
					<h2>一、canvas 简介</h2>
<p>​<code>&lt;canvas&gt;</code> 是 <code>HTML5</code> 新增的，一个可以使用脚本(通常为 <code>JavaScript</code>) 在其中绘制图像的 <code>HTML</code> 元素。它可以用来制作照片集或者制作简单(也不是那么简单)的动画，甚至可以进行实时视频处理和渲染。</p>
<p>​它最初由苹果内部使用自己 <code>MacOS X WebKit</code> 推出，供应用程序使用像仪表盘的构件和 <code>Safari</code> 浏览器使用。后来，有人通过 <code>Gecko</code> 内核的浏览器 (尤其是 <code>Mozilla</code>和<code>Firefox</code>)，<code>Opera</code> 和 <code>Chrome</code> 和超文本网络应用技术工作组建议为下一代的网络技术使用该元素。</p>
<p>​<code>Canvas</code> 是由 <code>HTML</code> 代码配合高度和宽度属性而定义出的可绘制区域。<code>JavaScript</code> 代码可以访问该区域，类似于其他通用的二维 <code>API</code>，通过一套完整的绘图函数来动态生成图形。</p>
<p>​	Mozilla 程序从 Gecko 1.8 (Firefox 1.5) 开始支持 <code>&lt;canvas&gt;</code>, Internet Explorer 从 IE9 开始 <code>&lt;canvas&gt;</code> 。Chrome 和 Opera 9+ 也支持 <code>&lt;canvas&gt;</code>。</p>
<hr>
<h2>二、Canvas基本使用</h2>

<pre>&lt;canvas id="tutorial" width="300" height="300"&gt;&lt;/canvas&gt;</pre>
<h3>2.1 <code>&lt;canvas&gt;</code> 元素</h3>
<p>​<code>&lt;canvas&gt;</code> 看起来和 <code>&lt;img&gt;</code> 标签一样，只是 <code>&lt;canvas&gt;</code> 只有两个可选的属性 <code>width、heigth</code> 属性，而没有 <code>src、alt</code> 属性。</p>
<p>​如果不给 <code>&lt;canvas&gt;</code> 设置 <code>widht、height</code> 属性时，则默认 <code>width</code>为300、<code>height</code> 为 150，单位都是 <code>px</code>。也可以使用 <code>css</code> 属性来设置宽高，但是如宽高属性和初始比例不一致，他会出现扭曲。所以，建议永远不要使用 <code>css</code> 属性来设置 <code>&lt;canvas&gt;</code> 的宽高。</p>
<p><strong>替换内容</strong></p>
<p>​由于某些较老的浏览器（尤其是 IE9 之前的 IE 浏览器）或者浏览器不支持 HTML 元素 <code>&lt;canvas&gt;</code>，在这些浏览器上你应该总是能展示替代内容。</p>
<p>​支持 <code>&lt;canvas&gt;</code> 的浏览器会只渲染  <code>&lt;canvas&gt;</code> 标签，而忽略其中的替代内容。不支持  <code>&lt;canvas&gt;</code> 的浏览器则 会直接渲染替代内容。</p>

<p>用文本替换：</p>
<pre>&lt;canvas&gt;
    你的浏览器不支持 canvas，请升级你的浏览器。
&lt;/canvas&gt;</pre>
<p>用 <code>&lt;img&gt;</code> 替换：</p>
<pre>&lt;canvas&gt;
    &lt;img decoding="async" src="./美女.jpg" alt=""&gt; 
&lt;/canvas&gt;</pre>
<p>结束标签 <code>&lt;/canvas&gt;</code> 不可省略。</p>
<p>与 <code>&lt;img&gt;</code> 元素不同，<code>&lt;canvas&gt;</code> 元素需要结束标签(<code>&lt;/canvas&gt;</code>)。如果结束标签不存在，则文档的其余部分会被认为是替代内容，将不会显示出来。</p>
<h3>2.2 渲染上下文(Thre Rending Context)</h3>
<p>​<code>&lt;canvas&gt;</code> 会创建一个固定大小的画布，会公开一个或多个<strong>渲染上下文</strong>(画笔)，使用<strong>渲染上下文</strong>来绘制和处理要展示的内容。</p>
<p>​	我们重点研究 2D 渲染上下文。  其他的上下文我们暂不研究，比如， WebGL 使用了基于 OpenGL ES的3D 上下文 ("experimental-webgl") 。</p>
<pre>var canvas = document.getElementById('tutorial');
//获得 2d 上下文对象
var ctx = canvas.getContext('2d');</pre>
<h3>2.3	检测支持性</h3>
<pre>var canvas = document.getElementById('tutorial');

if (canvas.getContext){
  var ctx = canvas.getContext('2d');
  // drawing code here
} else {
  // canvas-unsupported code here
}</pre>
<h3>2.4	代码模板</h3>
<div class="example">
<h2 class="example">实例</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-brackets">&lt;</span><span class="hl-reserved">canvas</span><span class="hl-code"> </span><span class="hl-var">id</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">tutorial</span><span class="hl-quotes">&quot;</span><span class="hl-code"> </span><span class="hl-var">width</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">300</span><span class="hl-quotes">&quot;</span><span class="hl-code"> </span><span class="hl-var">height</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">300</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">canvas</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">script</span><span class="hl-code"> </span><span class="hl-var">type</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">text/javascript</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code">
function draw(){
    var canvas = document.getElementById('tutorial');
    if(!canvas.getContext) return;
      var ctx = canvas.getContext(&quot;2d&quot;);
      //开始代码
    
}
draw();
</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">script</span><span class="hl-brackets">&gt;</span></div>
</div>
<br>
<a target="_blank" href="../try/try.php?filename=html5-canvas-intro" class="tryitbtn" rel="noopener noreferrer">尝试一下 »</a>

</div>

<h3>2.5 一个简单的例子</h3>
<p>以下实例绘制两个长方形：</p>
<div class="example">
<h2 class="example">实例</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-brackets">&lt;</span><span class="hl-reserved">canvas</span><span class="hl-code"> </span><span class="hl-var">id</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">tutorial</span><span class="hl-quotes">&quot;</span><span class="hl-code"> </span><span class="hl-var">width</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">300</span><span class="hl-quotes">&quot;</span><span class="hl-code"> </span><span class="hl-var">height</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">300</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">canvas</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">script</span><span class="hl-code"> </span><span class="hl-var">type</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">text/javascript</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;</span><span class="hl-code">
function draw(){
    var canvas = document.getElementById('tutorial');
    if(!canvas.getContext) return;
    var ctx = canvas.getContext(&quot;2d&quot;);
    ctx.fillStyle = &quot;rgb(200,0,0)&quot;;
      //绘制矩形
    ctx.fillRect (10, 10, 55, 50);
 
    ctx.fillStyle = &quot;rgba(0, 0, 200, 0.5)&quot;;
    ctx.fillRect (30, 30, 55, 50);
}
draw();
</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">script</span><span class="hl-brackets">&gt;</span></div>
</div>
<br>
<a target="_blank" href="../try/try.php?filename=html5-canvas-intro2" class="tryitbtn" rel="noopener noreferrer">尝试一下 »</a>
</div><hr>
<h2>三、绘制形状</h2>
<h3>3.1	栅格 <code>(grid)</code> 和坐标空间</h3>
<p>​如下图所示，<code>canvas</code> 元素默认被网格所覆盖。通常来说网格中的一个单元相当于 <code>canvas</code> 元素中的一像素。栅格的起点为左上角，坐标为 (0,0) 。所有元素的位置都相对于原点来定位。所以图中蓝色方形左上角的坐标为距离左边（X 轴）x 像素，距离上边（Y  轴）y 像素，坐标为 (x,y)。</p>
<p>​后面我们会涉及到坐标原点的平移、网格的旋转以及缩放等。</p>

<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2018/12/Canvas_default_grid.png"></p>
<h3>3.2	绘制矩形</h3>
<p>​<code>&lt;canvas&gt;</code> 只支持一种原生的图形绘制：<strong>矩形</strong>。所有其他图形都至少需要生成一种路径 (<code>path</code>)。不过，我们拥有众多路径生成的方法让复杂图形的绘制成为了可能。</p>
<p>canvast 提供了三种方法绘制矩形：</p>
<ul><li>
1、<span class="marked">fillRect(x, y, width, height)</span>：绘制一个填充的矩形。
</li><li>
2、<span class="marked">strokeRect(x, y, width, height)</span>：绘制一个矩形的边框。
</li><li>
3、<span class="marked">clearRect(x, y, widh, height)</span>：清除指定的矩形区域，然后这块区域会变的完全透明。
</li></ul>
<p><strong>说明：</strong>这 3 个方法具有相同的参数。</p>
<ul><li>
<strong>x, y</strong>：指的是矩形的左上角的坐标。(相对于canvas的坐标原点)
</li><li>
<strong>width, height</strong>：指的是绘制的矩形的宽和高。
</li></ul>
<div class="example"><div class="example_code"><div class="hl-main"><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">canvas</span><span class="hl-code"> = </span><span class="hl-builtin">document</span><span class="hl-code">.</span><span class="hl-identifier">getElementById</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">tutorial</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-reserved">if</span><span class="hl-brackets">(</span><span class="hl-code">!</span><span class="hl-identifier">canvas</span><span class="hl-code">.</span><span class="hl-identifier">getContext</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-reserved">return</span><span class="hl-code">;
    </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">ctx</span><span class="hl-code"> = </span><span class="hl-identifier">canvas</span><span class="hl-code">.</span><span class="hl-identifier">getContext</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">2d</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">fillRect</span><span class="hl-brackets">(</span><span class="hl-number">10</span><span class="hl-code">, </span><span class="hl-number">10</span><span class="hl-code">, </span><span class="hl-number">100</span><span class="hl-code">, </span><span class="hl-number">50</span><span class="hl-brackets">)</span><span class="hl-code">;     </span><span class="hl-comment">//</span><span class="hl-comment"> 绘制矩形，填充的默认颜色为黑色</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">strokeRect</span><span class="hl-brackets">(</span><span class="hl-number">10</span><span class="hl-code">, </span><span class="hl-number">70</span><span class="hl-code">, </span><span class="hl-number">100</span><span class="hl-code">, </span><span class="hl-number">50</span><span class="hl-brackets">)</span><span class="hl-code">;   </span><span class="hl-comment">//</span><span class="hl-comment"> 绘制矩形边框</span><span class="hl-comment"></span><span class="hl-code">
    
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;</span></div></div></div><p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2018/12/2400420933-5b74dd8f80306_articlex.png"></p>
<div class="example"><div class="example_code"><div class="hl-main"><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">clearRect</span><span class="hl-brackets">(</span><span class="hl-number">15</span><span class="hl-code">, </span><span class="hl-number">15</span><span class="hl-code">, </span><span class="hl-number">50</span><span class="hl-code">, </span><span class="hl-number">25</span><span class="hl-brackets">)</span><span class="hl-code">;</span></div></div></div>

<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2018/12/2347163070-5b74dd8f813a6_articlex.png"></p>
<hr>
<h2>四、绘制路径 (<code>path</code>)</h2>
<p>图形的基本元素是路径。</p>
<p>路径是通过不同颜色和宽度的线段或曲线相连形成的不同形状的点的集合。</p>
<p>一个路径，甚至一个子路径，都是闭合的。</p>
<p>使用路径绘制图形需要一些额外的步骤：</p>
<ol>
<li>创建路径起始点</li>
<li>调用绘制方法去绘制出路径</li>
<li>把路径封闭</li>
<li>一旦路径生成，通过描边或填充路径区域来渲染图形。</li>
</ol>
<p>下面是需要用到的方法：</p>
<ol>
<li>
<p><code>beginPath()</code></p>
<p>新建一条路径，路径一旦创建成功，图形绘制命令被指向到路径上生成路径</p>
</li>
<li>
<p><code>moveTo(x, y)</code></p>
<p>把画笔移动到指定的坐标<code>(x, y)</code>。相当于设置路径的起始点坐标。</p>
</li>
<li>
<p><code>closePath()</code></p>
<p>闭合路径之后，图形绘制命令又重新指向到上下文中</p>
</li>
<li>
<p><code>stroke()</code></p>
<p>通过线条来绘制图形轮廓</p>
</li>
<li>
<p><code>fill()</code></p>
<p>通过填充路径的内容区域生成实心的图形</p>
</li>
</ol>
<h3>4.1 绘制线段</h3>
<div class="example"><div class="example_code"><div class="hl-main"><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">canvas</span><span class="hl-code"> = </span><span class="hl-builtin">document</span><span class="hl-code">.</span><span class="hl-identifier">getElementById</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">tutorial</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-code">!</span><span class="hl-identifier">canvas</span><span class="hl-code">.</span><span class="hl-identifier">getContext</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-reserved">return</span><span class="hl-code">;
    </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">ctx</span><span class="hl-code"> = </span><span class="hl-identifier">canvas</span><span class="hl-code">.</span><span class="hl-identifier">getContext</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">2d</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">beginPath</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">//</span><span class="hl-comment">新建一条path</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">moveTo</span><span class="hl-brackets">(</span><span class="hl-number">50</span><span class="hl-code">, </span><span class="hl-number">50</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">//</span><span class="hl-comment">把画笔移动到指定的坐标</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">lineTo</span><span class="hl-brackets">(</span><span class="hl-number">200</span><span class="hl-code">, </span><span class="hl-number">50</span><span class="hl-brackets">)</span><span class="hl-code">;  </span><span class="hl-comment">//</span><span class="hl-comment">绘制一条从当前位置到指定坐标(200, 50)的直线.</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-comment">//</span><span class="hl-comment">闭合路径。会拉一条从当前点到path起始点的直线。如果当前点与起始点重合，则什么都不做</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">closePath</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">stroke</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">//</span><span class="hl-comment">绘制路径。</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;</span></div></div></div>
<h3>4.2 绘制三角形边框</h3>
<div class="example"><div class="example_code"><div class="hl-main"><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">canvas</span><span class="hl-code"> = </span><span class="hl-builtin">document</span><span class="hl-code">.</span><span class="hl-identifier">getElementById</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">tutorial</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-code">!</span><span class="hl-identifier">canvas</span><span class="hl-code">.</span><span class="hl-identifier">getContext</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-reserved">return</span><span class="hl-code">;
    </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">ctx</span><span class="hl-code"> = </span><span class="hl-identifier">canvas</span><span class="hl-code">.</span><span class="hl-identifier">getContext</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">2d</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">beginPath</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">moveTo</span><span class="hl-brackets">(</span><span class="hl-number">50</span><span class="hl-code">, </span><span class="hl-number">50</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">lineTo</span><span class="hl-brackets">(</span><span class="hl-number">200</span><span class="hl-code">, </span><span class="hl-number">50</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">lineTo</span><span class="hl-brackets">(</span><span class="hl-number">200</span><span class="hl-code">, </span><span class="hl-number">200</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">closePath</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">//</span><span class="hl-comment">虽然我们只绘制了两条线段，但是closePath会closePath，仍然是一个3角形</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">stroke</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">//</span><span class="hl-comment">描边。stroke不会自动closePath()</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;</span></div></div></div>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2018/12/2106846415-5b74dd8f67000_articlex.png"></p>
<h3>4.3 填充三角形</h3>
<div class="example"><div class="example_code"><div class="hl-main"><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">canvas</span><span class="hl-code"> = </span><span class="hl-builtin">document</span><span class="hl-code">.</span><span class="hl-identifier">getElementById</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">tutorial</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-code">!</span><span class="hl-identifier">canvas</span><span class="hl-code">.</span><span class="hl-identifier">getContext</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-reserved">return</span><span class="hl-code">;
    </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">ctx</span><span class="hl-code"> = </span><span class="hl-identifier">canvas</span><span class="hl-code">.</span><span class="hl-identifier">getContext</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">2d</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">beginPath</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">moveTo</span><span class="hl-brackets">(</span><span class="hl-number">50</span><span class="hl-code">, </span><span class="hl-number">50</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">lineTo</span><span class="hl-brackets">(</span><span class="hl-number">200</span><span class="hl-code">, </span><span class="hl-number">50</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">lineTo</span><span class="hl-brackets">(</span><span class="hl-number">200</span><span class="hl-code">, </span><span class="hl-number">200</span><span class="hl-brackets">)</span><span class="hl-code">;
   
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">fill</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">//</span><span class="hl-comment">填充闭合区域。如果path没有闭合，则fill()会自动闭合路径。</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;</span></div></div></div>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2018/12/3457015746-5b74dd8f72860_articlex.png"></p>
<h3>4.4 绘制圆弧</h3>
<p>有两个方法可以绘制圆弧：</p>
<p>1、<span class="marked">arc(x, y, r, startAngle, endAngle, anticlockwise)</span>: 以<code>(x, y)</code> 为圆心，以<code>r</code> 为半径，从 <code>startAngle</code> 弧度开始到<code>endAngle</code>弧度结束。<code>anticlosewise</code> 是布尔值，<code>true</code> 表示逆时针，<code>false</code> 表示顺时针(默认是顺时针)。</p>
<p>注意：</p>
<ol>
<li><p>这里的度数都是弧度。</p></li>
<li><p><code>0</code> 弧度是指的 <code>x</code> 轴正方向。</p>
<pre>radians=(Math.PI/180)*degrees   //角度转换成弧度</pre>
</li>
</ol>
<p>2、<span class="marked">arcTo(x1, y1, x2, y2, radius)</span>: 根据给定的控制点和半径画一段圆弧，最后再以直线连接两个控制点。</p>


<div class="example"><h2 class="example">圆弧案例 1</h2><div class="example_code"><div class="hl-main"><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">canvas</span><span class="hl-code"> = </span><span class="hl-builtin">document</span><span class="hl-code">.</span><span class="hl-identifier">getElementById</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">tutorial</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-code">!</span><span class="hl-identifier">canvas</span><span class="hl-code">.</span><span class="hl-identifier">getContext</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-reserved">return</span><span class="hl-code">;
    </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">ctx</span><span class="hl-code"> = </span><span class="hl-identifier">canvas</span><span class="hl-code">.</span><span class="hl-identifier">getContext</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">2d</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">beginPath</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">arc</span><span class="hl-brackets">(</span><span class="hl-number">50</span><span class="hl-code">, </span><span class="hl-number">50</span><span class="hl-code">, </span><span class="hl-number">40</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-builtin">Math</span><span class="hl-code">.</span><span class="hl-identifier">PI</span><span class="hl-code"> / </span><span class="hl-number">2</span><span class="hl-code">, </span><span class="hl-reserved">false</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">stroke</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;</span></div></div></div>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2018/12/3832141455-5b74dd8f658df_articlex.png"></p>
<div class="example"><h2 class="example">圆弧案例 2</h2><div class="example_code"><div class="hl-main"><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">canvas</span><span class="hl-code"> = </span><span class="hl-builtin">document</span><span class="hl-code">.</span><span class="hl-identifier">getElementById</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">tutorial</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-code">!</span><span class="hl-identifier">canvas</span><span class="hl-code">.</span><span class="hl-identifier">getContext</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-reserved">return</span><span class="hl-code">;
    </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">ctx</span><span class="hl-code"> = </span><span class="hl-identifier">canvas</span><span class="hl-code">.</span><span class="hl-identifier">getContext</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">2d</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">beginPath</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">arc</span><span class="hl-brackets">(</span><span class="hl-number">50</span><span class="hl-code">, </span><span class="hl-number">50</span><span class="hl-code">, </span><span class="hl-number">40</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-builtin">Math</span><span class="hl-code">.</span><span class="hl-identifier">PI</span><span class="hl-code"> / </span><span class="hl-number">2</span><span class="hl-code">, </span><span class="hl-reserved">false</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">stroke</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
 
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">beginPath</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">arc</span><span class="hl-brackets">(</span><span class="hl-number">150</span><span class="hl-code">, </span><span class="hl-number">50</span><span class="hl-code">, </span><span class="hl-number">40</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, -</span><span class="hl-builtin">Math</span><span class="hl-code">.</span><span class="hl-identifier">PI</span><span class="hl-code"> / </span><span class="hl-number">2</span><span class="hl-code">, </span><span class="hl-reserved">true</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">closePath</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">stroke</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
 
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">beginPath</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">arc</span><span class="hl-brackets">(</span><span class="hl-number">50</span><span class="hl-code">, </span><span class="hl-number">150</span><span class="hl-code">, </span><span class="hl-number">40</span><span class="hl-code">, -</span><span class="hl-builtin">Math</span><span class="hl-code">.</span><span class="hl-identifier">PI</span><span class="hl-code"> / </span><span class="hl-number">2</span><span class="hl-code">, </span><span class="hl-builtin">Math</span><span class="hl-code">.</span><span class="hl-identifier">PI</span><span class="hl-code"> / </span><span class="hl-number">2</span><span class="hl-code">, </span><span class="hl-reserved">false</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">fill</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
 
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">beginPath</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">arc</span><span class="hl-brackets">(</span><span class="hl-number">150</span><span class="hl-code">, </span><span class="hl-number">150</span><span class="hl-code">, </span><span class="hl-number">40</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-builtin">Math</span><span class="hl-code">.</span><span class="hl-identifier">PI</span><span class="hl-code">, </span><span class="hl-reserved">false</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">fill</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
 
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;</span></div></div></div>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2018/12/2218794221-5b74dd8f43f98_articlex.png"></p>
<div class="example"><h2 class="example">圆弧案例 3</h2><div class="example_code"><div class="hl-main"><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">canvas</span><span class="hl-code"> = </span><span class="hl-builtin">document</span><span class="hl-code">.</span><span class="hl-identifier">getElementById</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">tutorial</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-code">!</span><span class="hl-identifier">canvas</span><span class="hl-code">.</span><span class="hl-identifier">getContext</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-reserved">return</span><span class="hl-code">;
    </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">ctx</span><span class="hl-code"> = </span><span class="hl-identifier">canvas</span><span class="hl-code">.</span><span class="hl-identifier">getContext</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">2d</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">beginPath</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">moveTo</span><span class="hl-brackets">(</span><span class="hl-number">50</span><span class="hl-code">, </span><span class="hl-number">50</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-comment">//</span><span class="hl-comment">参数1、2：控制点1坐标   参数3、4：控制点2坐标  参数4：圆弧半径</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">arcTo</span><span class="hl-brackets">(</span><span class="hl-number">200</span><span class="hl-code">, </span><span class="hl-number">50</span><span class="hl-code">, </span><span class="hl-number">200</span><span class="hl-code">, </span><span class="hl-number">200</span><span class="hl-code">, </span><span class="hl-number">100</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">lineTo</span><span class="hl-brackets">(</span><span class="hl-number">200</span><span class="hl-code">, </span><span class="hl-number">200</span><span class="hl-brackets">)</span><span class="hl-code">
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">stroke</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">beginPath</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">rect</span><span class="hl-brackets">(</span><span class="hl-number">50</span><span class="hl-code">, </span><span class="hl-number">50</span><span class="hl-code">, </span><span class="hl-number">10</span><span class="hl-code">, </span><span class="hl-number">10</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">rect</span><span class="hl-brackets">(</span><span class="hl-number">200</span><span class="hl-code">, </span><span class="hl-number">50</span><span class="hl-code">, </span><span class="hl-number">10</span><span class="hl-code">, </span><span class="hl-number">10</span><span class="hl-brackets">)</span><span class="hl-code">
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">rect</span><span class="hl-brackets">(</span><span class="hl-number">200</span><span class="hl-code">, </span><span class="hl-number">200</span><span class="hl-code">, </span><span class="hl-number">10</span><span class="hl-code">, </span><span class="hl-number">10</span><span class="hl-brackets">)</span><span class="hl-code">
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">fill</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;</span></div></div></div>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2018/12/3556678928-5b74dd8f1bd2a_articlex.png"></p>
<p><code>arcTo</code> 方法的说明：</p>
<p>这个方法可以这样理解。绘制的弧形是由两条切线所决定。</p>
<p>第 1 条切线：起始点和控制点1决定的直线。</p>
<p>第 2 条切线：控制点1 和控制点2决定的直线。</p>
<p>​<strong>其实绘制的圆弧就是与这两条直线相切的圆弧。</strong></p>
<h3>4.5 绘制贝塞尔曲线</h3>
<p><strong>4.5.1 什么是贝塞尔曲线</strong></p>
​<p>贝塞尔曲线(Bézier curve)，又称贝兹曲线或贝济埃曲线，是应用于二维图形应用程序的数学曲线。</p>

<p>一般的矢量图形软件通过它来精确画出曲线，贝兹曲线由线段与节点组成，节点是可拖动的支点，线段像可伸缩的皮筋，我们在绘图工具上看到的钢笔工具就是来做这种矢量曲线的。</p>

<p>贝塞尔曲线是计算机图形学中相当重要的参数曲线，在一些比较成熟的位图软件中也有贝塞尔曲线工具如 PhotoShop 等。在 Flash4 中还没有完整的曲线工具，而在 Flash5 里面已经提供出贝塞尔曲线工具。</p>

<p>贝塞尔曲线于 1962，由法国工程师皮埃尔·贝塞尔（Pierre Bézier）所广泛发表，他运用贝塞尔曲线来为汽车的主体进行设计。贝塞尔曲线最初由Paul de Casteljau 于 1959 年运用 de Casteljau 演算法开发，以稳定数值的方法求出贝兹曲线。</p>
<p>一次贝塞尔曲线其实是一条直线</p>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2018/12/240px-b_1_big.gif"></p>
<p>二次贝塞尔曲线</p>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2018/12/b_2_big.gif"></p>
<p><img decoding="async" src="/wp-content/uploads/2018/12/1544764428-5713-240px-BC3A9zier-2-big.svg-.png"></p>
<p>三次贝塞尔曲线</p>
<p><img decoding="async" src="/wp-content/uploads/2018/12/b_3_big.gif"></p>
<p><img decoding="async" src="/wp-content/uploads/2018/12/1544764428-2467-240px-BC3A9zier-3-big.svg-.png"></p>
<p><strong>4.5.2 绘制贝塞尔曲线</strong></p>
<p>绘制二次贝塞尔曲线:</p>
<pre>quadraticCurveTo(cp1x, cp1y, x, y)</pre>

<p>说明：</p>
<ul><li>
​ 参数 1 和 2：控制点坐标
</li><li>
​ 参数 3 和 4：结束点坐标</li></ul>
<div class="example"><div class="example_code"><div class="hl-main"><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">canvas</span><span class="hl-code"> = </span><span class="hl-builtin">document</span><span class="hl-code">.</span><span class="hl-identifier">getElementById</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">tutorial</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-code">!</span><span class="hl-identifier">canvas</span><span class="hl-code">.</span><span class="hl-identifier">getContext</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-reserved">return</span><span class="hl-code">;
    </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">ctx</span><span class="hl-code"> = </span><span class="hl-identifier">canvas</span><span class="hl-code">.</span><span class="hl-identifier">getContext</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">2d</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">beginPath</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">moveTo</span><span class="hl-brackets">(</span><span class="hl-number">10</span><span class="hl-code">, </span><span class="hl-number">200</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">//</span><span class="hl-comment">起始点</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">cp1x</span><span class="hl-code"> = </span><span class="hl-number">40</span><span class="hl-code">, </span><span class="hl-identifier">cp1y</span><span class="hl-code"> = </span><span class="hl-number">100</span><span class="hl-code">;  </span><span class="hl-comment">//</span><span class="hl-comment">控制点</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">x</span><span class="hl-code"> = </span><span class="hl-number">200</span><span class="hl-code">, </span><span class="hl-identifier">y</span><span class="hl-code"> = </span><span class="hl-number">200</span><span class="hl-code">; </span><span class="hl-comment">//</span><span class="hl-comment"> 结束点</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-comment">//</span><span class="hl-comment">绘制二次贝塞尔曲线</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">quadraticCurveTo</span><span class="hl-brackets">(</span><span class="hl-identifier">cp1x</span><span class="hl-code">, </span><span class="hl-identifier">cp1y</span><span class="hl-code">, </span><span class="hl-identifier">x</span><span class="hl-code">, </span><span class="hl-identifier">y</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">stroke</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
 
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">beginPath</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">rect</span><span class="hl-brackets">(</span><span class="hl-number">10</span><span class="hl-code">, </span><span class="hl-number">200</span><span class="hl-code">, </span><span class="hl-number">10</span><span class="hl-code">, </span><span class="hl-number">10</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">rect</span><span class="hl-brackets">(</span><span class="hl-identifier">cp1x</span><span class="hl-code">, </span><span class="hl-identifier">cp1y</span><span class="hl-code">, </span><span class="hl-number">10</span><span class="hl-code">, </span><span class="hl-number">10</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">rect</span><span class="hl-brackets">(</span><span class="hl-identifier">x</span><span class="hl-code">, </span><span class="hl-identifier">y</span><span class="hl-code">, </span><span class="hl-number">10</span><span class="hl-code">, </span><span class="hl-number">10</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">fill</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
 
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;</span></div></div></div>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2018/12/274915666-5b74dd8ecb2e2_articlex.png"></p>
<p>绘制三次贝塞尔曲线:</p>
<pre>bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y)</pre>
<p>说明：</p>
<ul><li>
​ 参数 1 和 2：控制点 1 的坐标
</li><li>
​ 参数 3 和 4：控制点 2 的坐标
</li><li>
​ 参数 5 和 6：结束点的坐标
</li></ul>
<div class="example"><div class="example_code"><div class="hl-main"><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">canvas</span><span class="hl-code"> = </span><span class="hl-builtin">document</span><span class="hl-code">.</span><span class="hl-identifier">getElementById</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">tutorial</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-code">!</span><span class="hl-identifier">canvas</span><span class="hl-code">.</span><span class="hl-identifier">getContext</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-reserved">return</span><span class="hl-code">;
    </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">ctx</span><span class="hl-code"> = </span><span class="hl-identifier">canvas</span><span class="hl-code">.</span><span class="hl-identifier">getContext</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">2d</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">beginPath</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">moveTo</span><span class="hl-brackets">(</span><span class="hl-number">40</span><span class="hl-code">, </span><span class="hl-number">200</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">//</span><span class="hl-comment">起始点</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">cp1x</span><span class="hl-code"> = </span><span class="hl-number">20</span><span class="hl-code">, </span><span class="hl-identifier">cp1y</span><span class="hl-code"> = </span><span class="hl-number">100</span><span class="hl-code">;  </span><span class="hl-comment">//</span><span class="hl-comment">控制点1</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">cp2x</span><span class="hl-code"> = </span><span class="hl-number">100</span><span class="hl-code">, </span><span class="hl-identifier">cp2y</span><span class="hl-code"> = </span><span class="hl-number">120</span><span class="hl-code">;  </span><span class="hl-comment">//</span><span class="hl-comment">控制点2</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">x</span><span class="hl-code"> = </span><span class="hl-number">200</span><span class="hl-code">, </span><span class="hl-identifier">y</span><span class="hl-code"> = </span><span class="hl-number">200</span><span class="hl-code">; </span><span class="hl-comment">//</span><span class="hl-comment"> 结束点</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-comment">//</span><span class="hl-comment">绘制二次贝塞尔曲线</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">bezierCurveTo</span><span class="hl-brackets">(</span><span class="hl-identifier">cp1x</span><span class="hl-code">, </span><span class="hl-identifier">cp1y</span><span class="hl-code">, </span><span class="hl-identifier">cp2x</span><span class="hl-code">, </span><span class="hl-identifier">cp2y</span><span class="hl-code">, </span><span class="hl-identifier">x</span><span class="hl-code">, </span><span class="hl-identifier">y</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">stroke</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
 
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">beginPath</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">rect</span><span class="hl-brackets">(</span><span class="hl-number">40</span><span class="hl-code">, </span><span class="hl-number">200</span><span class="hl-code">, </span><span class="hl-number">10</span><span class="hl-code">, </span><span class="hl-number">10</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">rect</span><span class="hl-brackets">(</span><span class="hl-identifier">cp1x</span><span class="hl-code">, </span><span class="hl-identifier">cp1y</span><span class="hl-code">, </span><span class="hl-number">10</span><span class="hl-code">, </span><span class="hl-number">10</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">rect</span><span class="hl-brackets">(</span><span class="hl-identifier">cp2x</span><span class="hl-code">, </span><span class="hl-identifier">cp2y</span><span class="hl-code">, </span><span class="hl-number">10</span><span class="hl-code">, </span><span class="hl-number">10</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">rect</span><span class="hl-brackets">(</span><span class="hl-identifier">x</span><span class="hl-code">, </span><span class="hl-identifier">y</span><span class="hl-code">, </span><span class="hl-number">10</span><span class="hl-code">, </span><span class="hl-number">10</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">fill</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
 
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;</span></div></div></div>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2018/12/3947786617-5b74dd8ec8678_articlex.png"></p>
<hr>
<h2>五、添加样式和颜色</h2>
<p>​ 在前面的绘制矩形章节中，只用到了默认的线条和颜色。</p>
<p>​ 如果想要给图形上色，有两个重要的属性可以做到。</p>
<ol>
<li>
<p><code>fillStyle = color</code> 设置图形的填充颜色</p>
</li>
<li>
<p><code>strokeStyle = color</code> 设置图形轮廓的颜色</p>
</li>
</ol>
<p><strong>备注：</strong></p>
<ul><li>
1. color 可以是表示 css 颜色值的字符串、渐变对象或者图案对象。</li>
<li>
2. 默认情况下，线条和填充颜色都是黑色。</li>
<li>
3. 一旦您设置了 strokeStyle 或者  fillStyle 的值，那么这个新值就会成为新绘制的图形的默认值。如果你要给每个图形上不同的颜色，你需要重新设置 fillStyle 或 strokeStyle 的值。</li>
</ul>
<h3>fillStyle</h3>
<div class="example"><div class="example_code"><div class="hl-main"><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
  </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">canvas</span><span class="hl-code"> = </span><span class="hl-builtin">document</span><span class="hl-code">.</span><span class="hl-identifier">getElementById</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">tutorial</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
  </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-code">!</span><span class="hl-identifier">canvas</span><span class="hl-code">.</span><span class="hl-identifier">getContext</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-reserved">return</span><span class="hl-code">;
  </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">ctx</span><span class="hl-code"> = </span><span class="hl-identifier">canvas</span><span class="hl-code">.</span><span class="hl-identifier">getContext</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">2d</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;
  </span><span class="hl-reserved">for</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">i</span><span class="hl-code"> = </span><span class="hl-number">0</span><span class="hl-code">; </span><span class="hl-identifier">i</span><span class="hl-code"> &lt; </span><span class="hl-number">6</span><span class="hl-code">; </span><span class="hl-identifier">i</span><span class="hl-code">++</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">for</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">j</span><span class="hl-code"> = </span><span class="hl-number">0</span><span class="hl-code">; </span><span class="hl-identifier">j</span><span class="hl-code"> &lt; </span><span class="hl-number">6</span><span class="hl-code">; </span><span class="hl-identifier">j</span><span class="hl-code">++</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">fillStyle</span><span class="hl-code"> = </span><span class="hl-quotes">'</span><span class="hl-string">rgb(</span><span class="hl-quotes">'</span><span class="hl-code"> + </span><span class="hl-builtin">Math</span><span class="hl-code">.</span><span class="hl-identifier">floor</span><span class="hl-brackets">(</span><span class="hl-number">255</span><span class="hl-code"> - </span><span class="hl-number">42.5</span><span class="hl-code"> * </span><span class="hl-identifier">i</span><span class="hl-brackets">)</span><span class="hl-code"> + </span><span class="hl-quotes">'</span><span class="hl-string">,</span><span class="hl-quotes">'</span><span class="hl-code"> +
        </span><span class="hl-builtin">Math</span><span class="hl-code">.</span><span class="hl-identifier">floor</span><span class="hl-brackets">(</span><span class="hl-number">255</span><span class="hl-code"> - </span><span class="hl-number">42.5</span><span class="hl-code"> * </span><span class="hl-identifier">j</span><span class="hl-brackets">)</span><span class="hl-code"> + </span><span class="hl-quotes">'</span><span class="hl-string">,0)</span><span class="hl-quotes">'</span><span class="hl-code">;
      </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">fillRect</span><span class="hl-brackets">(</span><span class="hl-identifier">j</span><span class="hl-code"> * </span><span class="hl-number">50</span><span class="hl-code">, </span><span class="hl-identifier">i</span><span class="hl-code"> * </span><span class="hl-number">50</span><span class="hl-code">, </span><span class="hl-number">50</span><span class="hl-code">, </span><span class="hl-number">50</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">
  </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;</span></div></div></div>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2018/12/2505008676-5b74dd8ebad41_articlex.png"></p>
<h3>strokeStyle</h3>

<div class="example"><div class="example_code"><div class="hl-main"><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">canvas</span><span class="hl-code"> = </span><span class="hl-builtin">document</span><span class="hl-code">.</span><span class="hl-identifier">getElementById</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">tutorial</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-code">!</span><span class="hl-identifier">canvas</span><span class="hl-code">.</span><span class="hl-identifier">getContext</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-reserved">return</span><span class="hl-code">;
    </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">ctx</span><span class="hl-code"> = </span><span class="hl-identifier">canvas</span><span class="hl-code">.</span><span class="hl-identifier">getContext</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">2d</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-reserved">for</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">i</span><span class="hl-code"> = </span><span class="hl-number">0</span><span class="hl-code">; </span><span class="hl-identifier">i</span><span class="hl-code"> &lt; </span><span class="hl-number">6</span><span class="hl-code">; </span><span class="hl-identifier">i</span><span class="hl-code">++</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-reserved">for</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">j</span><span class="hl-code"> = </span><span class="hl-number">0</span><span class="hl-code">; </span><span class="hl-identifier">j</span><span class="hl-code"> &lt; </span><span class="hl-number">6</span><span class="hl-code">; </span><span class="hl-identifier">j</span><span class="hl-code">++</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
            </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">strokeStyle</span><span class="hl-code"> = `</span><span class="hl-identifier">rgb</span><span class="hl-brackets">(</span><span class="hl-code">$</span><span class="hl-brackets">{</span><span class="hl-identifier">randomInt</span><span class="hl-brackets">(</span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">255</span><span class="hl-brackets">)</span><span class="hl-brackets">}</span><span class="hl-code">,$</span><span class="hl-brackets">{</span><span class="hl-identifier">randomInt</span><span class="hl-brackets">(</span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">255</span><span class="hl-brackets">)</span><span class="hl-brackets">}</span><span class="hl-code">,$</span><span class="hl-brackets">{</span><span class="hl-identifier">randomInt</span><span class="hl-brackets">(</span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">255</span><span class="hl-brackets">)</span><span class="hl-brackets">}</span><span class="hl-brackets">)</span><span class="hl-code">`;
            </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">strokeRect</span><span class="hl-brackets">(</span><span class="hl-identifier">j</span><span class="hl-code"> * </span><span class="hl-number">50</span><span class="hl-code">, </span><span class="hl-identifier">i</span><span class="hl-code"> * </span><span class="hl-number">50</span><span class="hl-code">, </span><span class="hl-number">40</span><span class="hl-code">, </span><span class="hl-number">40</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-brackets">}</span><span class="hl-code">
    </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-comment">/*</span><span class="hl-comment">*
 返回随机的 [from, to] 之间的整数(包括from，也包括to)
 </span><span class="hl-comment">*/</span><span class="hl-code">
</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">randomInt</span><span class="hl-brackets">(</span><span class="hl-identifier">from</span><span class="hl-code">, </span><span class="hl-identifier">to</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">parseInt</span><span class="hl-brackets">(</span><span class="hl-builtin">Math</span><span class="hl-code">.</span><span class="hl-identifier">random</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> * </span><span class="hl-brackets">(</span><span class="hl-identifier">to</span><span class="hl-code"> - </span><span class="hl-identifier">from</span><span class="hl-code"> + </span><span class="hl-number">1</span><span class="hl-brackets">)</span><span class="hl-code"> + </span><span class="hl-identifier">from</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span></div></div></div>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2018/12/3288535670-5b74dd8ea12d9_articlex.png"></p>
<h3>Transparency(透明度)</h3>
<p><strong>globalAlpha = transparencyValue</strong>: 这个属性影响到 canvas 里所有图形的透明度，有效的值范围是 0.0 （完全透明）到 1.0（完全不透明），默认是 1.0。</p>

<p>​ <strong>globalAlpha</strong> 属性在需要绘制大量拥有相同透明度的图形时候相当高效。不过，我认为使用rgba()设置透明度更加好一些。</p><h3>1、line style</h3>
<p>线宽。只能是正值。默认是 1.0。</p>

<p>起始点和终点的连线为中心，<strong>上下各占线宽的一半</strong>。</p>
<div class="example"><div class="example_code"><div class="hl-main"><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">beginPath</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">moveTo</span><span class="hl-brackets">(</span><span class="hl-number">10</span><span class="hl-code">, </span><span class="hl-number">10</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">lineTo</span><span class="hl-brackets">(</span><span class="hl-number">100</span><span class="hl-code">, </span><span class="hl-number">10</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">lineWidth</span><span class="hl-code"> = </span><span class="hl-number">10</span><span class="hl-code">;
</span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">stroke</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
 
</span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">beginPath</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">moveTo</span><span class="hl-brackets">(</span><span class="hl-number">110</span><span class="hl-code">, </span><span class="hl-number">10</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">lineTo</span><span class="hl-brackets">(</span><span class="hl-number">160</span><span class="hl-code">, </span><span class="hl-number">10</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">lineWidth</span><span class="hl-code"> = </span><span class="hl-number">20</span><span class="hl-code">;
</span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">stroke</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span></div></div></div>


<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2018/12/3410060825-5b74dd8ea12d9_articlex.png" alt="" /></p>
<h3>2. lineCap = type</h3>
<p>线条末端样式。</p>

<p>共有 3 个值：</p>
<ol>
<li>
<p><code>butt</code>：线段末端以方形结束</p>
</li>
<li>
<p><code>round</code>：线段末端以圆形结束</p>
</li>
<li><p><code>square</code>：线段末端以方形结束，但是增加了一个宽度和线段相同，高度是线段厚度一半的矩形区域。</p>
<div class="example"><div class="example_code"><div class="hl-main"><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">lineCaps</span><span class="hl-code"> = </span><span class="hl-brackets">[</span><span class="hl-quotes">&quot;</span><span class="hl-string">butt</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-quotes">&quot;</span><span class="hl-string">round</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-quotes">&quot;</span><span class="hl-string">square</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">]</span><span class="hl-code">;
 
</span><span class="hl-reserved">for</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">i</span><span class="hl-code"> = </span><span class="hl-number">0</span><span class="hl-code">; </span><span class="hl-identifier">i</span><span class="hl-code"> &lt; </span><span class="hl-number">3</span><span class="hl-code">; </span><span class="hl-identifier">i</span><span class="hl-code">++</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">beginPath</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">moveTo</span><span class="hl-brackets">(</span><span class="hl-number">20</span><span class="hl-code"> + </span><span class="hl-number">30</span><span class="hl-code"> * </span><span class="hl-identifier">i</span><span class="hl-code">, </span><span class="hl-number">30</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">lineTo</span><span class="hl-brackets">(</span><span class="hl-number">20</span><span class="hl-code"> + </span><span class="hl-number">30</span><span class="hl-code"> * </span><span class="hl-identifier">i</span><span class="hl-code">, </span><span class="hl-number">100</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">lineWidth</span><span class="hl-code"> = </span><span class="hl-number">20</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">lineCap</span><span class="hl-code"> = </span><span class="hl-identifier">lineCaps</span><span class="hl-brackets">[</span><span class="hl-identifier">i</span><span class="hl-brackets">]</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">stroke</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
 
</span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">beginPath</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">moveTo</span><span class="hl-brackets">(</span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">30</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">lineTo</span><span class="hl-brackets">(</span><span class="hl-number">300</span><span class="hl-code">, </span><span class="hl-number">30</span><span class="hl-brackets">)</span><span class="hl-code">;
 
</span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">moveTo</span><span class="hl-brackets">(</span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">100</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">lineTo</span><span class="hl-brackets">(</span><span class="hl-number">300</span><span class="hl-code">, </span><span class="hl-number">100</span><span class="hl-brackets">)</span><span class="hl-code">
 
</span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">strokeStyle</span><span class="hl-code"> = </span><span class="hl-quotes">&quot;</span><span class="hl-string">red</span><span class="hl-quotes">&quot;</span><span class="hl-code">;
</span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">lineWidth</span><span class="hl-code"> = </span><span class="hl-number">1</span><span class="hl-code">;
</span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">stroke</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;</span></div></div></div>
</ul>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2018/12/3380216230-5b74dd8e97e85_articlex.png"></p>

<h3>3. lineJoin = type</h3>
<p>同一个 path 内，设定线条与线条间接合处的样式。</p>
<p>共有 3 个值 <code>round</code>, <code>bevel</code> 和 <code>miter</code>：</p>
<ol>
<li>
<p><code>round</code> 通过填充一个额外的，圆心在相连部分末端的扇形，绘制拐角的形状。 圆角的半径是线段的宽度。</p>
</li>
<li>
<p><code>bevel</code> 在相连部分的末端填充一个额外的以三角形为底的区域， 每个部分都有各自独立的矩形拐角。</p>
</li>
<li>
<p><code>miter</code>(默认) 通过延伸相连部分的外边缘，使其相交于一点，形成一个额外的菱形区域。</p>
<div class="example"><div class="example_code"><div class="hl-main"><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">canvas</span><span class="hl-code"> = </span><span class="hl-builtin">document</span><span class="hl-code">.</span><span class="hl-identifier">getElementById</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">tutorial</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-code">!</span><span class="hl-identifier">canvas</span><span class="hl-code">.</span><span class="hl-identifier">getContext</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-reserved">return</span><span class="hl-code">;
    </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">ctx</span><span class="hl-code"> = </span><span class="hl-identifier">canvas</span><span class="hl-code">.</span><span class="hl-identifier">getContext</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">2d</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;
 
    </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">lineJoin</span><span class="hl-code"> = </span><span class="hl-brackets">[</span><span class="hl-quotes">'</span><span class="hl-string">round</span><span class="hl-quotes">'</span><span class="hl-code">, </span><span class="hl-quotes">'</span><span class="hl-string">bevel</span><span class="hl-quotes">'</span><span class="hl-code">, </span><span class="hl-quotes">'</span><span class="hl-string">miter</span><span class="hl-quotes">'</span><span class="hl-brackets">]</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">lineWidth</span><span class="hl-code"> = </span><span class="hl-number">20</span><span class="hl-code">;
 
    </span><span class="hl-reserved">for</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">i</span><span class="hl-code"> = </span><span class="hl-number">0</span><span class="hl-code">; </span><span class="hl-identifier">i</span><span class="hl-code"> &lt; </span><span class="hl-identifier">lineJoin</span><span class="hl-code">.</span><span class="hl-identifier">length</span><span class="hl-code">; </span><span class="hl-identifier">i</span><span class="hl-code">++</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">lineJoin</span><span class="hl-code"> = </span><span class="hl-identifier">lineJoin</span><span class="hl-brackets">[</span><span class="hl-identifier">i</span><span class="hl-brackets">]</span><span class="hl-code">;
        </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">beginPath</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">moveTo</span><span class="hl-brackets">(</span><span class="hl-number">50</span><span class="hl-code">, </span><span class="hl-number">50</span><span class="hl-code"> + </span><span class="hl-identifier">i</span><span class="hl-code"> * </span><span class="hl-number">50</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">lineTo</span><span class="hl-brackets">(</span><span class="hl-number">100</span><span class="hl-code">, </span><span class="hl-number">100</span><span class="hl-code"> + </span><span class="hl-identifier">i</span><span class="hl-code"> * </span><span class="hl-number">50</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">lineTo</span><span class="hl-brackets">(</span><span class="hl-number">150</span><span class="hl-code">, </span><span class="hl-number">50</span><span class="hl-code"> + </span><span class="hl-identifier">i</span><span class="hl-code"> * </span><span class="hl-number">50</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">lineTo</span><span class="hl-brackets">(</span><span class="hl-number">200</span><span class="hl-code">, </span><span class="hl-number">100</span><span class="hl-code"> + </span><span class="hl-identifier">i</span><span class="hl-code"> * </span><span class="hl-number">50</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">lineTo</span><span class="hl-brackets">(</span><span class="hl-number">250</span><span class="hl-code">, </span><span class="hl-number">50</span><span class="hl-code"> + </span><span class="hl-identifier">i</span><span class="hl-code"> * </span><span class="hl-number">50</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">stroke</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">
 
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;</span></div></div></div>

</li>
</ul>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2018/12/1584506777-5b74dd8e82768_articlex.png"></p>
<h3>4. 虚线</h3>
<p>用 <code>setLineDash</code> 方法和 <code>lineDashOffset</code> 属性来制定虚线样式。 <code>setLineDash</code> 方法接受一个数组，来指定线段与间隙的交替；<code>lineDashOffset</code>属性设置起始偏移量。</p>
<div class="example"><div class="example_code"><div class="hl-main"><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">canvas</span><span class="hl-code"> = </span><span class="hl-builtin">document</span><span class="hl-code">.</span><span class="hl-identifier">getElementById</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">tutorial</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-code">!</span><span class="hl-identifier">canvas</span><span class="hl-code">.</span><span class="hl-identifier">getContext</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-reserved">return</span><span class="hl-code">;
    </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">ctx</span><span class="hl-code"> = </span><span class="hl-identifier">canvas</span><span class="hl-code">.</span><span class="hl-identifier">getContext</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">2d</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;
 
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">setLineDash</span><span class="hl-brackets">(</span><span class="hl-brackets">[</span><span class="hl-number">20</span><span class="hl-code">, </span><span class="hl-number">5</span><span class="hl-brackets">]</span><span class="hl-brackets">)</span><span class="hl-code">;  </span><span class="hl-comment">//</span><span class="hl-comment"> [实线长度, 间隙长度]</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">lineDashOffset</span><span class="hl-code"> = -</span><span class="hl-number">0</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">strokeRect</span><span class="hl-brackets">(</span><span class="hl-number">50</span><span class="hl-code">, </span><span class="hl-number">50</span><span class="hl-code">, </span><span class="hl-number">210</span><span class="hl-code">, </span><span class="hl-number">210</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;</span></div></div></div>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2018/12/2805401035-5b74dd8e6833c_articlex.png"></p>
<p><strong>备注</strong>： getLineDash() 返回一个包含当前虚线样式，长度为非负偶数的数组。</p><hr>

<h2>六、绘制文本</h2>
<h3>绘制文本的两个方法</h3>
<p>canvas 提供了两种方法来渲染文本:</p>
<ol>
<li>
<p><code>fillText(text, x, y [, maxWidth])</code> 在指定的 (x,y) 位置填充指定的文本，绘制的最大宽度是可选的。</p>
</li>
<li>
<p><code>strokeText(text, x, y [, maxWidth])</code> 在指定的 (x,y) 位置绘制文本边框，绘制的最大宽度是可选的。</p>
<div class="example"><div class="example_code"><div class="hl-main"><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">ctx</span><span class="hl-code">;
</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">canvas</span><span class="hl-code"> = </span><span class="hl-builtin">document</span><span class="hl-code">.</span><span class="hl-identifier">getElementById</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">tutorial</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-code">!</span><span class="hl-identifier">canvas</span><span class="hl-code">.</span><span class="hl-identifier">getContext</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-reserved">return</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code"> = </span><span class="hl-identifier">canvas</span><span class="hl-code">.</span><span class="hl-identifier">getContext</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">2d</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">font</span><span class="hl-code"> = </span><span class="hl-quotes">&quot;</span><span class="hl-string">100px sans-serif</span><span class="hl-quotes">&quot;</span><span class="hl-code">
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">fillText</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">天若有情</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-number">10</span><span class="hl-code">, </span><span class="hl-number">100</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">strokeText</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">天若有情</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-number">10</span><span class="hl-code">, </span><span class="hl-number">200</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;</span></div></div></div></li></ul>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2018/12/404304980-5b74dd8e7499e_articlex.png"></p>
<h3>给文本添加样式</h3>
<ol>
<li>
<p><code>font = value</code> 当前我们用来绘制文本的样式。这个字符串使用和 <code>CSS font</code> 属性相同的语法。 默认的字体是 <code>10px sans-serif</code>。</p>
</li>
<li>
<p><code>textAlign = value</code> 文本对齐选项。 可选的值包括：<code>start</code>, <code>end</code>, <code>left</code>, <code>right</code> or <code>center</code>。 默认值是 <code>start</code>。</p>
</li>
<li>
<p><code>textBaseline = value</code> 基线对齐选项，可选的值包括：<code>top</code>, <code>hanging</code>, <code>middle</code>, <code>alphabetic</code>, <code>ideographic</code>, <code>bottom</code>。默认值是 <code>alphabetic。</code>。</p>
</li>
<li>
<p><code>direction = value</code> 文本方向。可能的值包括：<code>ltr</code>, <code>rtl</code>, <code>inherit</code>。默认值是 <code>inherit</code>。</p>
</li>
</ol><hr>
<h2>七、绘制图片</h2>
<p>​ 我们也可以在 <code>canvas</code> 上直接绘制图片。</p>
<h3>7.1 由零开始创建图片</h3>
<div class="example"><div class="example_code"><div class="hl-main"><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">img</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-builtin">Image</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;   </span><span class="hl-comment">//</span><span class="hl-comment"> 创建一个&lt;img&gt;元素</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-identifier">img</span><span class="hl-code">.</span><span class="hl-identifier">src</span><span class="hl-code"> = </span><span class="hl-quotes">'</span><span class="hl-string">myImage.png</span><span class="hl-quotes">'</span><span class="hl-code">; </span><span class="hl-comment">//</span><span class="hl-comment"> 设置图片源地址</span><span class="hl-comment"></span></div></div></div><p>脚本执行后图片开始装载。</p>

<p><strong>绘制 <code>img</code></strong></p>
<pre>// 参数 1：要绘制的 img  
// 参数 2、3：绘制的 img 在 canvas 中的坐标
ctx.drawImage(img,0,0); </pre>
<p><strong>注意</strong>：考虑到图片是从网络加载，如果 <code>drawImage</code> 的时候图片还没有完全加载完成，则什么都不做，个别浏览器会抛异常。所以我们应该保证在 <code>img</code> 绘制完成之后再 <code>drawImage</code>。</p>
<div class="example"><div class="example_code"><div class="hl-main"><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">img</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-builtin">Image</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;   </span><span class="hl-comment">//</span><span class="hl-comment"> 创建img元素</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-identifier">img</span><span class="hl-code">.</span><span class="hl-identifier">onload</span><span class="hl-code"> = </span><span class="hl-reserved">function</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">drawImage</span><span class="hl-brackets">(</span><span class="hl-identifier">img</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-identifier">img</span><span class="hl-code">.</span><span class="hl-identifier">src</span><span class="hl-code"> = </span><span class="hl-quotes">'</span><span class="hl-string">myImage.png</span><span class="hl-quotes">'</span><span class="hl-code">; </span><span class="hl-comment">//</span><span class="hl-comment"> 设置图片源地址</span><span class="hl-comment"></span></div></div></div>
<h3>7.2 绘制 <code>img</code> 标签元素中的图片</h3>
<p>​<code>img</code> 可以 <code>new</code> 也可以来源于我们页面的 <code>&lt;img&gt;</code>标签。</p>
<div class="example"><div class="example_code"><div class="hl-main"><span class="hl-code">&lt;</span><span class="hl-identifier">img</span><span class="hl-code"> </span><span class="hl-identifier">decoding</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">async</span><span class="hl-quotes">&quot;</span><span class="hl-code"> </span><span class="hl-identifier">src</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">./美女.jpg</span><span class="hl-quotes">&quot;</span><span class="hl-code"> </span><span class="hl-identifier">alt</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-quotes">&quot;</span><span class="hl-code"> </span><span class="hl-identifier">width</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">300</span><span class="hl-quotes">&quot;</span><span class="hl-code">&gt;&lt;</span><span class="hl-identifier">br</span><span class="hl-code">&gt;
&lt;</span><span class="hl-identifier">canvas</span><span class="hl-code"> </span><span class="hl-identifier">id</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">tutorial</span><span class="hl-quotes">&quot;</span><span class="hl-code"> </span><span class="hl-identifier">width</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">600</span><span class="hl-quotes">&quot;</span><span class="hl-code"> </span><span class="hl-identifier">height</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">400</span><span class="hl-quotes">&quot;</span><span class="hl-code">&gt;&lt;/</span><span class="hl-identifier">canvas</span><span class="hl-code">&gt;
</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">canvas</span><span class="hl-code"> = </span><span class="hl-builtin">document</span><span class="hl-code">.</span><span class="hl-identifier">getElementById</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">tutorial</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-code">!</span><span class="hl-identifier">canvas</span><span class="hl-code">.</span><span class="hl-identifier">getContext</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-reserved">return</span><span class="hl-code">;
    </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">ctx</span><span class="hl-code"> = </span><span class="hl-identifier">canvas</span><span class="hl-code">.</span><span class="hl-identifier">getContext</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">2d</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">img</span><span class="hl-code"> = </span><span class="hl-builtin">document</span><span class="hl-code">.</span><span class="hl-identifier">querySelector</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">img</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">drawImage</span><span class="hl-brackets">(</span><span class="hl-identifier">img</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-builtin">document</span><span class="hl-code">.</span><span class="hl-identifier">querySelector</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">img</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">.</span><span class="hl-identifier">onclick</span><span class="hl-code"> = </span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span></div></div></div>
<p>第一张图片就是页面中的 <code>&lt;img&gt;</code> 标签：</p>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2018/12/2255709523-5b74dd8eb033e_articlex.png"></p>
<h3>7.3 缩放图片</h3>
<p><code>drawImage()</code> 也可以再添加两个参数：</p>
<pre>drawImage(image, x, y, width, height)</pre>
<p>​这个方法多了 2 个参数：<code>width</code> 和 <code>height</code>，这两个参数用来控制 当像 canvas 画入时应该缩放的大小。</p>
<pre>ctx.drawImage(img, 0, 0, 400, 200)</pre>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2018/12/327614951-5b74dd8e71ab1_articlex.png"></p>
<h3>7.4 切片(slice)</h3>
<pre>drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight)</pre>
<p>第一个参数和其它的是相同的，都是一个图像或者另一个 canvas 的引用。</p>

<p>其他 8 个参数：</p>

<p>前 4 个是定义图像源的切片位置和大小，后 4 个则是定义切片的目标显示位置和大小。</p>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2018/12/2106688680-54566fa3d81dc_articlex.jpeg"></p>
<hr>
<h2>八、状态的保存和恢复</h2>
<p><code>Saving and restoring state</code> 是绘制复杂图形时必不可少的操作。</p>
<p><code>save()</code> 和 <code>restore()</code></p>
<p><code>save</code> 和 <code>restore</code> 方法是用来保存和恢复 <code>canvas</code> 状态的，都没有参数。</p>
<p>​<code>Canvas</code> 的状态就是当前画面应用的所有样式和变形的一个快照。</p>
<p>1、关于 save() ：Canvas状态存储在栈中，每当save()方法被调用后，当前的状态就被推送到栈中保存。</p>
<p>一个绘画状态包括：</p>
<ul>
<li>
<p>当前应用的变形（即移动，旋转和缩放）</p>
</li>
<li>
<p><code>strokeStyle</code>, <code>fillStyle</code>, <code>globalAlpha</code>, <code>lineWidth</code>, <code>lineCap</code>, <code>lineJoin</code>, <code>miterLimit</code>, <code>shadowOffsetX</code>, <code>shadowOffsetY</code>, <code>shadowBlur</code>, <code>shadowColor</code>, <code>globalCompositeOperation 的值</code></p>
</li>
<li>
<p>当前的裁切路径（<code>clipping path</code>）</p>
<p>​</p>
<p>可以调用任意多次 <code>save</code>方法(类似数组的 <code>push()</code>)。</p>
</li>
</ul><p><strong>可以调用任意多次 <code>save</code>方法</strong>(类似数组的<code>push()</code>)。</p>
<p>2、关于restore()：每一次调用 restore 方法，上一个保存的状态就从栈中弹出，所有设定都恢复(类似数组的 <code>pop()</code>)。</p>
<div class="example"><div class="example_code"><div class="hl-main"><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">ctx</span><span class="hl-code">;
</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">canvas</span><span class="hl-code"> = </span><span class="hl-builtin">document</span><span class="hl-code">.</span><span class="hl-identifier">getElementById</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">tutorial</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-code">!</span><span class="hl-identifier">canvas</span><span class="hl-code">.</span><span class="hl-identifier">getContext</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-reserved">return</span><span class="hl-code">;
    </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">ctx</span><span class="hl-code"> = </span><span class="hl-identifier">canvas</span><span class="hl-code">.</span><span class="hl-identifier">getContext</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">2d</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;
 
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">fillRect</span><span class="hl-brackets">(</span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">150</span><span class="hl-code">, </span><span class="hl-number">150</span><span class="hl-brackets">)</span><span class="hl-code">;   </span><span class="hl-comment">//</span><span class="hl-comment"> 使用默认设置绘制一个矩形</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">save</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;                  </span><span class="hl-comment">//</span><span class="hl-comment"> 保存默认状态</span><span class="hl-comment"></span><span class="hl-code">
 
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">fillStyle</span><span class="hl-code"> = </span><span class="hl-quotes">'</span><span class="hl-string">red</span><span class="hl-quotes">'</span><span class="hl-code">       </span><span class="hl-comment">//</span><span class="hl-comment"> 在原有配置基础上对颜色做改变</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">fillRect</span><span class="hl-brackets">(</span><span class="hl-number">15</span><span class="hl-code">, </span><span class="hl-number">15</span><span class="hl-code">, </span><span class="hl-number">120</span><span class="hl-code">, </span><span class="hl-number">120</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">//</span><span class="hl-comment"> 使用新的设置绘制一个矩形</span><span class="hl-comment"></span><span class="hl-code">
 
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">save</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;                  </span><span class="hl-comment">//</span><span class="hl-comment"> 保存当前状态</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">fillStyle</span><span class="hl-code"> = </span><span class="hl-quotes">'</span><span class="hl-string">#FFF</span><span class="hl-quotes">'</span><span class="hl-code">       </span><span class="hl-comment">//</span><span class="hl-comment"> 再次改变颜色配置</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">fillRect</span><span class="hl-brackets">(</span><span class="hl-number">30</span><span class="hl-code">, </span><span class="hl-number">30</span><span class="hl-code">, </span><span class="hl-number">90</span><span class="hl-code">, </span><span class="hl-number">90</span><span class="hl-brackets">)</span><span class="hl-code">;   </span><span class="hl-comment">//</span><span class="hl-comment"> 使用新的配置绘制一个矩形</span><span class="hl-comment"></span><span class="hl-code">
 
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">restore</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;               </span><span class="hl-comment">//</span><span class="hl-comment"> 重新加载之前的颜色状态</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">fillRect</span><span class="hl-brackets">(</span><span class="hl-number">45</span><span class="hl-code">, </span><span class="hl-number">45</span><span class="hl-code">, </span><span class="hl-number">60</span><span class="hl-code">, </span><span class="hl-number">60</span><span class="hl-brackets">)</span><span class="hl-code">;   </span><span class="hl-comment">//</span><span class="hl-comment"> 使用上一次的配置绘制一个矩形</span><span class="hl-comment"></span><span class="hl-code">
 
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">restore</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;               </span><span class="hl-comment">//</span><span class="hl-comment"> 加载默认颜色配置</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">fillRect</span><span class="hl-brackets">(</span><span class="hl-number">60</span><span class="hl-code">, </span><span class="hl-number">60</span><span class="hl-code">, </span><span class="hl-number">30</span><span class="hl-code">, </span><span class="hl-number">30</span><span class="hl-brackets">)</span><span class="hl-code">;   </span><span class="hl-comment">//</span><span class="hl-comment"> 使用加载的配置绘制一个矩形</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;</span></div></div></div>
<hr>
<h2>九、变形</h2>
<h3>9.1 translate</h3>
<p><code>translate(x, y)</code></p>
<p>用来移动 <code>canvas</code> 的<strong>原点</strong>到指定的位置</p>
<p>​<code>translate</code> 方法接受两个参数。<code>x</code> 是左右偏移量，<code>y</code> 是上下偏移量，如右图所示。</p>
<p>在做变形之前先保存状态是一个良好的习惯。大多数情况下，调用 <code>restore</code> 方法比手动恢复原先的状态要简单得多。又如果你是在一个循环中做位移但没有保存和恢复 <code>canvas</code> 的状态，很可能到最后会发现怎么有些东西不见了，那是因为它很可能已经超出  <code>canvas</code> 范围以外了。</p>
<p>​ 注意：<code>translate</code> 移动的是 <code>canvas</code> 的坐标原点(坐标变换)。</p>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2018/12/829832336-5b74dd8e3ad9a_articlex.png"></p>
<div class="example"><div class="example_code"><div class="hl-main"><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">ctx</span><span class="hl-code">;
</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">canvas</span><span class="hl-code"> = </span><span class="hl-builtin">document</span><span class="hl-code">.</span><span class="hl-identifier">getElementById</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">tutorial1</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-code">!</span><span class="hl-identifier">canvas</span><span class="hl-code">.</span><span class="hl-identifier">getContext</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-reserved">return</span><span class="hl-code">;
    </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">ctx</span><span class="hl-code"> = </span><span class="hl-identifier">canvas</span><span class="hl-code">.</span><span class="hl-identifier">getContext</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">2d</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">save</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">//</span><span class="hl-comment">保存坐原点平移之前的状态</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">translate</span><span class="hl-brackets">(</span><span class="hl-number">100</span><span class="hl-code">, </span><span class="hl-number">100</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">strokeRect</span><span class="hl-brackets">(</span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">100</span><span class="hl-code">, </span><span class="hl-number">100</span><span class="hl-brackets">)</span><span class="hl-code">
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">restore</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">//</span><span class="hl-comment">恢复到最初状态</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">translate</span><span class="hl-brackets">(</span><span class="hl-number">220</span><span class="hl-code">, </span><span class="hl-number">220</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">fillRect</span><span class="hl-brackets">(</span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">100</span><span class="hl-code">, </span><span class="hl-number">100</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;</span></div></div></div>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2018/12/1230266743-5b74dd8e3b0ce_articlex.png"></p>
<h3>9.2 rotate</h3>
<pre>rotate(angle)</pre>

​ <p>旋转坐标轴。</p>

​ <p>这个方法只接受一个参数：旋转的角度(angle)，它是顺时针方向的，以弧度为单位的值。</p>

<p>​ 旋转的中心是坐标原点。</p>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2018/12/3322150878-5b74dd8e2b6a4_articlex.png"></p>
<div class="example"><div class="example_code"><div class="hl-main"><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">ctx</span><span class="hl-code">;
</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
  </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">canvas</span><span class="hl-code"> = </span><span class="hl-builtin">document</span><span class="hl-code">.</span><span class="hl-identifier">getElementById</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">tutorial1</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
  </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-code">!</span><span class="hl-identifier">canvas</span><span class="hl-code">.</span><span class="hl-identifier">getContext</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-reserved">return</span><span class="hl-code">;
  </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">ctx</span><span class="hl-code"> = </span><span class="hl-identifier">canvas</span><span class="hl-code">.</span><span class="hl-identifier">getContext</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">2d</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;
 
  </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">fillStyle</span><span class="hl-code"> = </span><span class="hl-quotes">&quot;</span><span class="hl-string">red</span><span class="hl-quotes">&quot;</span><span class="hl-code">;
  </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">save</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
 
  </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">translate</span><span class="hl-brackets">(</span><span class="hl-number">100</span><span class="hl-code">, </span><span class="hl-number">100</span><span class="hl-brackets">)</span><span class="hl-code">;
  </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">rotate</span><span class="hl-brackets">(</span><span class="hl-builtin">Math</span><span class="hl-code">.</span><span class="hl-identifier">PI</span><span class="hl-code"> / </span><span class="hl-number">180</span><span class="hl-code"> * </span><span class="hl-number">45</span><span class="hl-brackets">)</span><span class="hl-code">;
  </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">fillStyle</span><span class="hl-code"> = </span><span class="hl-quotes">&quot;</span><span class="hl-string">blue</span><span class="hl-quotes">&quot;</span><span class="hl-code">;
  </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">fillRect</span><span class="hl-brackets">(</span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">100</span><span class="hl-code">, </span><span class="hl-number">100</span><span class="hl-brackets">)</span><span class="hl-code">;
  </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">restore</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
 
  </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">save</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
  </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">translate</span><span class="hl-brackets">(</span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-brackets">)</span><span class="hl-code">;
  </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">fillRect</span><span class="hl-brackets">(</span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">50</span><span class="hl-code">, </span><span class="hl-number">50</span><span class="hl-brackets">)</span><span class="hl-code">
  </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">restore</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;</span></div></div></div>

<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2018/12/1819968878-5b74dd8e1e770_articlex.png"></p>
<h3>9.3 scale</h3>
<pre>scale(x, y)</pre>
<p>我们用它来增减图形在 <code>canvas</code> 中的像素数目，对形状，位图进行缩小或者放大。</p>
<p><code>scale</code>方法接受两个参数。<code>x,y</code> 分别是横轴和纵轴的缩放因子，它们都必须是正值。值比 1.0 小表示缩 小，比 1.0 大则表示放大，值为 1.0 时什么效果都没有。</p>
<p>​ 默认情况下，<code>canvas</code> 的 1 单位就是 1 个像素。举例说，如果我们设置缩放因子是 0.5，1 个单位就变成对应 0.5 个像素，这样绘制出来的形状就会是原先的一半。同理，设置为 2.0 时，1 个单位就对应变成了 2 像素，绘制的结果就是图形放大了 2 倍。</p>
<h3>9.4 transform (变形矩阵)</h3>
<pre>transform(a, b, c, d, e, f)</pre>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2018/12/2958376259-5b74dd8e15192_articlex.png"></p>
<ul><li>
a (m11): Horizontal scaling.
</li><li>
b (m12):  Horizontal skewing.
</li><li>
c (m21):​ Vertical skewing.
</li><li>
d (m22):​ Vertical scaling.
</li><li>
e (dx):​  Horizontal moving.
</li><li>
f (dy):​ Vertical moving.</li></ul>
<div class="example"><div class="example_code"><div class="hl-main"><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">ctx</span><span class="hl-code">;
</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">canvas</span><span class="hl-code"> = </span><span class="hl-builtin">document</span><span class="hl-code">.</span><span class="hl-identifier">getElementById</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">tutorial1</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-code">!</span><span class="hl-identifier">canvas</span><span class="hl-code">.</span><span class="hl-identifier">getContext</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-reserved">return</span><span class="hl-code">;
    </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">ctx</span><span class="hl-code"> = </span><span class="hl-identifier">canvas</span><span class="hl-code">.</span><span class="hl-identifier">getContext</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">2d</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">transform</span><span class="hl-brackets">(</span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">fillRect</span><span class="hl-brackets">(</span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">100</span><span class="hl-code">, </span><span class="hl-number">100</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;</span></div></div></div>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2018/12/489430190-5b74dd8e17ad2_articlex.png"></p><hr>
<h2>十、合成</h2>
​ <p>在前面的所有例子中、，我们总是将一个图形画在另一个之上，对于其他更多的情况，仅仅这样是远远不够的。比如，对合成的图形来说，绘制顺序会有限制。不过，我们可以利用 globalCompositeOperation 属性来改变这种状况。</p>

<pre>globalCompositeOperation = type</pre>
<div class="example"><div class="example_code"><div class="hl-main"><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">ctx</span><span class="hl-code">;
</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">canvas</span><span class="hl-code"> = </span><span class="hl-builtin">document</span><span class="hl-code">.</span><span class="hl-identifier">getElementById</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">tutorial1</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-code">!</span><span class="hl-identifier">canvas</span><span class="hl-code">.</span><span class="hl-identifier">getContext</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-reserved">return</span><span class="hl-code">;
    </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">ctx</span><span class="hl-code"> = </span><span class="hl-identifier">canvas</span><span class="hl-code">.</span><span class="hl-identifier">getContext</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">2d</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;
 
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">fillStyle</span><span class="hl-code"> = </span><span class="hl-quotes">&quot;</span><span class="hl-string">blue</span><span class="hl-quotes">&quot;</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">fillRect</span><span class="hl-brackets">(</span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">200</span><span class="hl-code">, </span><span class="hl-number">200</span><span class="hl-brackets">)</span><span class="hl-code">;
 
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">globalCompositeOperation</span><span class="hl-code"> = </span><span class="hl-quotes">&quot;</span><span class="hl-string">source-over</span><span class="hl-quotes">&quot;</span><span class="hl-code">; </span><span class="hl-comment">//</span><span class="hl-comment">全局合成操作</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">fillStyle</span><span class="hl-code"> = </span><span class="hl-quotes">&quot;</span><span class="hl-string">red</span><span class="hl-quotes">&quot;</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">fillRect</span><span class="hl-brackets">(</span><span class="hl-number">100</span><span class="hl-code">, </span><span class="hl-number">100</span><span class="hl-code">, </span><span class="hl-number">200</span><span class="hl-code">, </span><span class="hl-number">200</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;</span></div></div></div>
<p><strong>注</strong>：下面的展示中，蓝色是原有的，红色是新的。</p>

<p>type 是下面 13 种字符串值之一：</p>
<p>1、这是默认设置，新图像会覆盖在原有图像。</p>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2018/12/1858023544-5b74dd8e0813d.png"></p>
<p><p>2. source-in</p>
仅仅会出现新图像与原来图像重叠的部分，其他区域都变成透明的。(包括其他的老图像区域也会透明)</p>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2018/12/2183873141-5b74dd8e02a4a_articlex.png"></p>
<h3>3. source-out</h3>
<p>仅仅显示新图像与老图像没有重叠的部分，其余部分全部透明。(老图像也不显示)</p>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2018/12/2402253130-5b74dd8dd7621_articlex.png"></p>
<h3>4. source-atop</h3>
<p>新图像仅仅显示与老图像重叠区域。老图像仍然可以显示。</p>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2018/12/1206278247-5b74dd8dd9036_articlex.png"></p>
<h3>5. destination-over</h3>
<p>新图像会在老图像的下面。</p>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2018/12/2492190378-5b74dd8dca608_articlex.png"></p>
<h3>6. destination-in</h3>
<p>仅仅新老图像重叠部分的老图像被显示，其他区域全部透明。</p>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2018/12/284693590-5b74dd8dc7f3e_articlex.png"></p>
<h3>7. destination-out</h3>
<p>仅仅老图像与新图像没有重叠的部分。 注意显示的是老图像的部分区域。</p>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2018/12/1921976761-5b74dd8daba2d_articlex.png"></p>
<h3>8. destination-atop</h3>
<p>老图像仅仅仅仅显示重叠部分，新图像会显示在老图像的下面。</p>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2018/12/4055109887-5b74dd8db283c_articlex.png"></p>
<h3>9. lighter</h3>
<p>新老图像都显示，但是重叠区域的颜色做加处理。</p>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2018/12/1200224117-5b74dd8d9453e_articlex.png"></p>
<h3>10. darken</h3>
<p>保留重叠部分最黑的像素。(每个颜色位进行比较，得到最小的)</p>

<pre>blue: #0000ff
red: #ff0000</pre>

<p>所以重叠部分的颜色：<span class="marked">#000000</span>。</p>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2018/12/3835256030-5b74dd8d92ba5_articlex.png"></p>

<h3>11. lighten</h3>
<p>保证重叠部分最量的像素。(每个颜色位进行比较，得到最大的)</p>

<pre>blue: #0000ff
red: #ff0000</pre>
<p>
所以重叠部分的颜色：<span class="marked">#ff00ff</span>。</p>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2018/12/1617768463-5b74dd8d99843_articlex.png"></p>
<h3>12. xor</h3>
<p>重叠部分会变成透明。</p>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2018/12/2521026104-5b74dd8d6abd6_articlex.png"></p>
<h3>13. copy</h3>
<p>只有新图像会被保留，其余的全部被清除(边透明)。</p>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2018/12/2454891415-5b74dd8d67aec_articlex.png"></p><hr>
<h2>十一、裁剪路径</h2>
<pre>clip()</pre>

​ <p>把已经创建的路径转换成裁剪路径。</p>

​ <p>裁剪路径的作用是遮罩。只显示裁剪路径内的区域，裁剪路径外的区域会被隐藏。</p>

​ <p><strong>注意：</strong>clip() 只能遮罩在这个方法调用之后绘制的图像，如果是 clip() 方法调用之前绘制的图像，则无法实现遮罩。</p>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2018/12/2023283460-5b74dd8d67aec_articlex.png"></p>
<div class="example"><div class="example_code"><div class="hl-main"><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">ctx</span><span class="hl-code">;
</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">canvas</span><span class="hl-code"> = </span><span class="hl-builtin">document</span><span class="hl-code">.</span><span class="hl-identifier">getElementById</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">tutorial1</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-code">!</span><span class="hl-identifier">canvas</span><span class="hl-code">.</span><span class="hl-identifier">getContext</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-reserved">return</span><span class="hl-code">;
    </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">ctx</span><span class="hl-code"> = </span><span class="hl-identifier">canvas</span><span class="hl-code">.</span><span class="hl-identifier">getContext</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">2d</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;
 
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">beginPath</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">arc</span><span class="hl-brackets">(</span><span class="hl-number">20</span><span class="hl-code">,</span><span class="hl-number">20</span><span class="hl-code">, </span><span class="hl-number">100</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-builtin">Math</span><span class="hl-code">.</span><span class="hl-identifier">PI</span><span class="hl-code"> * </span><span class="hl-number">2</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">clip</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
 
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">fillStyle</span><span class="hl-code"> = </span><span class="hl-quotes">&quot;</span><span class="hl-string">pink</span><span class="hl-quotes">&quot;</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">fillRect</span><span class="hl-brackets">(</span><span class="hl-number">20</span><span class="hl-code">, </span><span class="hl-number">20</span><span class="hl-code">, </span><span class="hl-number">100</span><span class="hl-code">,</span><span class="hl-number">100</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;</span></div></div></div>
<hr>
<h2>十二、动画</h2>
<h3>动画的基本步骤</h3>
<ol>
<li><p>
<strong>清空 <code>canvas</code></strong> 再绘制每一帧动画之前，需要清空所有。清空所有最简单的做法就是 <code>clearRect()</code> 方法。</p>
</li>
<li><p>
<strong>保存 <code>canvas</code> 状态</strong> 如果在绘制的过程中会更改 <code>canvas</code> 的状态(颜色、移动了坐标原点等),又在绘制每一帧时都是原始状态的话，则最好保存下 <code>canvas</code> 的状态</p>
</li>
<li><p>
<strong>绘制动画图形</strong>这一步才是真正的绘制动画帧</p>
</li>
<li><p>
<strong>恢复 <code>canvas</code> 状态</strong>如果你前面保存了 <code>canvas</code> 状态，则应该在绘制完成一帧之后恢复  <code>canvas</code> 状态。</p>
</li>
</ol>
<h2>控制动画</h2>
<p>我们可用通过 <code>canvas</code> 的方法或者自定义的方法把图像会知道到 <code>canvas</code> 上。正常情况，我们能看到绘制的结果是在脚本执行结束之后。例如，我们不可能在一个 <code>for</code> 循环内部完成动画。</p>
<p>也就是，为了执行动画，我们需要一些可以定时执行重绘的方法。</p>
<p>一般用到下面三个方法：</p>
<ol>
<li><code>setInterval()</code></li>
<li><code>setTimeout()</code></li>
<li><code>requestAnimationFrame()</code></li>
</ol>
<div class="example"><h2 class="example">案例1：太阳系</h2><div class="example_code"><div class="hl-main"><span class="hl-identifier">let</span><span class="hl-code"> </span><span class="hl-identifier">sun</span><span class="hl-code">;
</span><span class="hl-identifier">let</span><span class="hl-code"> </span><span class="hl-identifier">earth</span><span class="hl-code">;
</span><span class="hl-identifier">let</span><span class="hl-code"> </span><span class="hl-identifier">moon</span><span class="hl-code">;
</span><span class="hl-identifier">let</span><span class="hl-code"> </span><span class="hl-identifier">ctx</span><span class="hl-code">;
</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">init</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">sun</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-builtin">Image</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">earth</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-builtin">Image</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">moon</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-builtin">Image</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">sun</span><span class="hl-code">.</span><span class="hl-identifier">src</span><span class="hl-code"> = </span><span class="hl-quotes">&quot;</span><span class="hl-string">sun.png</span><span class="hl-quotes">&quot;</span><span class="hl-code">;
    </span><span class="hl-identifier">earth</span><span class="hl-code">.</span><span class="hl-identifier">src</span><span class="hl-code"> = </span><span class="hl-quotes">&quot;</span><span class="hl-string">earth.png</span><span class="hl-quotes">&quot;</span><span class="hl-code">;
    </span><span class="hl-identifier">moon</span><span class="hl-code">.</span><span class="hl-identifier">src</span><span class="hl-code"> = </span><span class="hl-quotes">&quot;</span><span class="hl-string">moon.png</span><span class="hl-quotes">&quot;</span><span class="hl-code">;
 
    </span><span class="hl-identifier">let</span><span class="hl-code"> </span><span class="hl-identifier">canvas</span><span class="hl-code"> = </span><span class="hl-builtin">document</span><span class="hl-code">.</span><span class="hl-identifier">querySelector</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">#solar</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code"> = </span><span class="hl-identifier">canvas</span><span class="hl-code">.</span><span class="hl-identifier">getContext</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">2d</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;
 
    </span><span class="hl-identifier">sun</span><span class="hl-code">.</span><span class="hl-identifier">onload</span><span class="hl-code"> = </span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">
    </span><span class="hl-brackets">}</span><span class="hl-code">
 
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-identifier">init</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">clearRect</span><span class="hl-brackets">(</span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">300</span><span class="hl-code">, </span><span class="hl-number">300</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">//</span><span class="hl-comment">清空所有的内容</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-comment">/*</span><span class="hl-comment">绘制 太阳</span><span class="hl-comment">*/</span><span class="hl-code">
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">drawImage</span><span class="hl-brackets">(</span><span class="hl-identifier">sun</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">300</span><span class="hl-code">, </span><span class="hl-number">300</span><span class="hl-brackets">)</span><span class="hl-code">;
 
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">save</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">translate</span><span class="hl-brackets">(</span><span class="hl-number">150</span><span class="hl-code">, </span><span class="hl-number">150</span><span class="hl-brackets">)</span><span class="hl-code">;
 
    </span><span class="hl-comment">//</span><span class="hl-comment">绘制earth轨道</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">beginPath</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">strokeStyle</span><span class="hl-code"> = </span><span class="hl-quotes">&quot;</span><span class="hl-string">rgba(255,255,0,0.5)</span><span class="hl-quotes">&quot;</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">arc</span><span class="hl-brackets">(</span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">100</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">2</span><span class="hl-code"> * </span><span class="hl-builtin">Math</span><span class="hl-code">.</span><span class="hl-identifier">PI</span><span class="hl-brackets">)</span><span class="hl-code">
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">stroke</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">
 
    </span><span class="hl-identifier">let</span><span class="hl-code"> </span><span class="hl-identifier">time</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-builtin">Date</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-comment">//</span><span class="hl-comment">绘制地球</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">rotate</span><span class="hl-brackets">(</span><span class="hl-number">2</span><span class="hl-code"> * </span><span class="hl-builtin">Math</span><span class="hl-code">.</span><span class="hl-identifier">PI</span><span class="hl-code"> / </span><span class="hl-number">60</span><span class="hl-code"> * </span><span class="hl-identifier">time</span><span class="hl-code">.</span><span class="hl-identifier">getSeconds</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> + </span><span class="hl-number">2</span><span class="hl-code"> * </span><span class="hl-builtin">Math</span><span class="hl-code">.</span><span class="hl-identifier">PI</span><span class="hl-code"> / </span><span class="hl-number">60000</span><span class="hl-code"> * </span><span class="hl-identifier">time</span><span class="hl-code">.</span><span class="hl-identifier">getMilliseconds</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">translate</span><span class="hl-brackets">(</span><span class="hl-number">100</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">drawImage</span><span class="hl-brackets">(</span><span class="hl-identifier">earth</span><span class="hl-code">, -</span><span class="hl-number">12</span><span class="hl-code">, -</span><span class="hl-number">12</span><span class="hl-brackets">)</span><span class="hl-code">
 
    </span><span class="hl-comment">//</span><span class="hl-comment">绘制月球轨道</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">beginPath</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">strokeStyle</span><span class="hl-code"> = </span><span class="hl-quotes">&quot;</span><span class="hl-string">rgba(255,255,255,.3)</span><span class="hl-quotes">&quot;</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">arc</span><span class="hl-brackets">(</span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">40</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">2</span><span class="hl-code"> * </span><span class="hl-builtin">Math</span><span class="hl-code">.</span><span class="hl-identifier">PI</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">stroke</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
 
    </span><span class="hl-comment">//</span><span class="hl-comment">绘制月球</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">rotate</span><span class="hl-brackets">(</span><span class="hl-number">2</span><span class="hl-code"> * </span><span class="hl-builtin">Math</span><span class="hl-code">.</span><span class="hl-identifier">PI</span><span class="hl-code"> / </span><span class="hl-number">6</span><span class="hl-code"> * </span><span class="hl-identifier">time</span><span class="hl-code">.</span><span class="hl-identifier">getSeconds</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> + </span><span class="hl-number">2</span><span class="hl-code"> * </span><span class="hl-builtin">Math</span><span class="hl-code">.</span><span class="hl-identifier">PI</span><span class="hl-code"> / </span><span class="hl-number">6000</span><span class="hl-code"> * </span><span class="hl-identifier">time</span><span class="hl-code">.</span><span class="hl-identifier">getMilliseconds</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">translate</span><span class="hl-brackets">(</span><span class="hl-number">40</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">drawImage</span><span class="hl-brackets">(</span><span class="hl-identifier">moon</span><span class="hl-code">, -</span><span class="hl-number">3.5</span><span class="hl-code">, -</span><span class="hl-number">3.5</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">restore</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
 
    </span><span class="hl-identifier">requestAnimationFrame</span><span class="hl-brackets">(</span><span class="hl-identifier">draw</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span></div></div></div>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2018/12/796853783-5b74dd8f41e21_articlex.gif"></p>

<div class="example"><h2 class="example">案例2：模拟时钟</h2><div class="example_code"><div class="hl-main"><span class="hl-identifier">init</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
 
</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">init</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">let</span><span class="hl-code"> </span><span class="hl-identifier">canvas</span><span class="hl-code"> = </span><span class="hl-builtin">document</span><span class="hl-code">.</span><span class="hl-identifier">querySelector</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">#solar</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">let</span><span class="hl-code"> </span><span class="hl-identifier">ctx</span><span class="hl-code"> = </span><span class="hl-identifier">canvas</span><span class="hl-code">.</span><span class="hl-identifier">getContext</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">2d</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-identifier">ctx</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
 
</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-identifier">ctx</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">requestAnimationFrame</span><span class="hl-brackets">(</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">step</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-identifier">drawDial</span><span class="hl-brackets">(</span><span class="hl-identifier">ctx</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">//</span><span class="hl-comment">绘制表盘</span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-identifier">drawAllHands</span><span class="hl-brackets">(</span><span class="hl-identifier">ctx</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">//</span><span class="hl-comment">绘制时分秒针</span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-identifier">requestAnimationFrame</span><span class="hl-brackets">(</span><span class="hl-identifier">step</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-comment">/*</span><span class="hl-comment">绘制时分秒针</span><span class="hl-comment">*/</span><span class="hl-code">
</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">drawAllHands</span><span class="hl-brackets">(</span><span class="hl-identifier">ctx</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">let</span><span class="hl-code"> </span><span class="hl-identifier">time</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-builtin">Date</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
 
    </span><span class="hl-identifier">let</span><span class="hl-code"> </span><span class="hl-identifier">s</span><span class="hl-code"> = </span><span class="hl-identifier">time</span><span class="hl-code">.</span><span class="hl-identifier">getSeconds</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">let</span><span class="hl-code"> </span><span class="hl-identifier">m</span><span class="hl-code"> = </span><span class="hl-identifier">time</span><span class="hl-code">.</span><span class="hl-identifier">getMinutes</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">let</span><span class="hl-code"> </span><span class="hl-identifier">h</span><span class="hl-code"> = </span><span class="hl-identifier">time</span><span class="hl-code">.</span><span class="hl-identifier">getHours</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
 
    </span><span class="hl-identifier">let</span><span class="hl-code"> </span><span class="hl-identifier">pi</span><span class="hl-code"> = </span><span class="hl-builtin">Math</span><span class="hl-code">.</span><span class="hl-identifier">PI</span><span class="hl-code">;
    </span><span class="hl-identifier">let</span><span class="hl-code"> </span><span class="hl-identifier">secondAngle</span><span class="hl-code"> = </span><span class="hl-identifier">pi</span><span class="hl-code"> / </span><span class="hl-number">180</span><span class="hl-code"> * </span><span class="hl-number">6</span><span class="hl-code"> * </span><span class="hl-identifier">s</span><span class="hl-code">;  </span><span class="hl-comment">//</span><span class="hl-comment">计算出来s针的弧度</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">let</span><span class="hl-code"> </span><span class="hl-identifier">minuteAngle</span><span class="hl-code"> = </span><span class="hl-identifier">pi</span><span class="hl-code"> / </span><span class="hl-number">180</span><span class="hl-code"> * </span><span class="hl-number">6</span><span class="hl-code"> * </span><span class="hl-identifier">m</span><span class="hl-code"> + </span><span class="hl-identifier">secondAngle</span><span class="hl-code"> / </span><span class="hl-number">60</span><span class="hl-code">;  </span><span class="hl-comment">//</span><span class="hl-comment">计算出来分针的弧度</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">let</span><span class="hl-code"> </span><span class="hl-identifier">hourAngle</span><span class="hl-code"> = </span><span class="hl-identifier">pi</span><span class="hl-code"> / </span><span class="hl-number">180</span><span class="hl-code"> * </span><span class="hl-number">30</span><span class="hl-code"> * </span><span class="hl-identifier">h</span><span class="hl-code"> + </span><span class="hl-identifier">minuteAngle</span><span class="hl-code"> / </span><span class="hl-number">12</span><span class="hl-code">;  </span><span class="hl-comment">//</span><span class="hl-comment">计算出来时针的弧度</span><span class="hl-comment"></span><span class="hl-code">
 
    </span><span class="hl-identifier">drawHand</span><span class="hl-brackets">(</span><span class="hl-identifier">hourAngle</span><span class="hl-code">, </span><span class="hl-number">60</span><span class="hl-code">, </span><span class="hl-number">6</span><span class="hl-code">, </span><span class="hl-quotes">&quot;</span><span class="hl-string">red</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-identifier">ctx</span><span class="hl-brackets">)</span><span class="hl-code">;  </span><span class="hl-comment">//</span><span class="hl-comment">绘制时针</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">drawHand</span><span class="hl-brackets">(</span><span class="hl-identifier">minuteAngle</span><span class="hl-code">, </span><span class="hl-number">106</span><span class="hl-code">, </span><span class="hl-number">4</span><span class="hl-code">, </span><span class="hl-quotes">&quot;</span><span class="hl-string">green</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-identifier">ctx</span><span class="hl-brackets">)</span><span class="hl-code">;  </span><span class="hl-comment">//</span><span class="hl-comment">绘制分针</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">drawHand</span><span class="hl-brackets">(</span><span class="hl-identifier">secondAngle</span><span class="hl-code">, </span><span class="hl-number">129</span><span class="hl-code">, </span><span class="hl-number">2</span><span class="hl-code">, </span><span class="hl-quotes">&quot;</span><span class="hl-string">blue</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-identifier">ctx</span><span class="hl-brackets">)</span><span class="hl-code">;  </span><span class="hl-comment">//</span><span class="hl-comment">绘制秒针</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-comment">/*</span><span class="hl-comment">绘制时针、或分针、或秒针
 * 参数1：要绘制的针的角度
 * 参数2：要绘制的针的长度
 * 参数3：要绘制的针的宽度
 * 参数4：要绘制的针的颜色
 * 参数4：ctx
 * </span><span class="hl-comment">*/</span><span class="hl-code">
</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">drawHand</span><span class="hl-brackets">(</span><span class="hl-identifier">angle</span><span class="hl-code">, </span><span class="hl-identifier">len</span><span class="hl-code">, </span><span class="hl-identifier">width</span><span class="hl-code">, </span><span class="hl-identifier">color</span><span class="hl-code">, </span><span class="hl-identifier">ctx</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">save</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">translate</span><span class="hl-brackets">(</span><span class="hl-number">150</span><span class="hl-code">, </span><span class="hl-number">150</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">//</span><span class="hl-comment">把坐标轴的远点平移到原来的中心</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">rotate</span><span class="hl-brackets">(</span><span class="hl-code">-</span><span class="hl-builtin">Math</span><span class="hl-code">.</span><span class="hl-identifier">PI</span><span class="hl-code"> / </span><span class="hl-number">2</span><span class="hl-code"> + </span><span class="hl-identifier">angle</span><span class="hl-brackets">)</span><span class="hl-code">;  </span><span class="hl-comment">//</span><span class="hl-comment">旋转坐标轴。 x轴就是针的角度</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">beginPath</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">moveTo</span><span class="hl-brackets">(</span><span class="hl-code">-</span><span class="hl-number">4</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">lineTo</span><span class="hl-brackets">(</span><span class="hl-identifier">len</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-brackets">)</span><span class="hl-code">;  </span><span class="hl-comment">//</span><span class="hl-comment"> 沿着x轴绘制针</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">lineWidth</span><span class="hl-code"> = </span><span class="hl-identifier">width</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">strokeStyle</span><span class="hl-code"> = </span><span class="hl-identifier">color</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">lineCap</span><span class="hl-code"> = </span><span class="hl-quotes">&quot;</span><span class="hl-string">round</span><span class="hl-quotes">&quot;</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">stroke</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">closePath</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">restore</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
 
</span><span class="hl-comment">/*</span><span class="hl-comment">绘制表盘</span><span class="hl-comment">*/</span><span class="hl-code">
</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">drawDial</span><span class="hl-brackets">(</span><span class="hl-identifier">ctx</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">let</span><span class="hl-code"> </span><span class="hl-identifier">pi</span><span class="hl-code"> = </span><span class="hl-builtin">Math</span><span class="hl-code">.</span><span class="hl-identifier">PI</span><span class="hl-code">;
 
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">clearRect</span><span class="hl-brackets">(</span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">300</span><span class="hl-code">, </span><span class="hl-number">300</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">//</span><span class="hl-comment">清除所有内容</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">save</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
 
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">translate</span><span class="hl-brackets">(</span><span class="hl-number">150</span><span class="hl-code">, </span><span class="hl-number">150</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">//</span><span class="hl-comment">一定坐标原点到原来的中心</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">beginPath</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">arc</span><span class="hl-brackets">(</span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">148</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">2</span><span class="hl-code"> * </span><span class="hl-identifier">pi</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">//</span><span class="hl-comment">绘制圆周</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">stroke</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">closePath</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
 
    </span><span class="hl-reserved">for</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">let</span><span class="hl-code"> </span><span class="hl-identifier">i</span><span class="hl-code"> = </span><span class="hl-number">0</span><span class="hl-code">; </span><span class="hl-identifier">i</span><span class="hl-code"> &lt; </span><span class="hl-number">60</span><span class="hl-code">; </span><span class="hl-identifier">i</span><span class="hl-code">++</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-comment">//</span><span class="hl-comment">绘制刻度。</span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">save</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">rotate</span><span class="hl-brackets">(</span><span class="hl-code">-</span><span class="hl-identifier">pi</span><span class="hl-code"> / </span><span class="hl-number">2</span><span class="hl-code"> + </span><span class="hl-identifier">i</span><span class="hl-code"> * </span><span class="hl-identifier">pi</span><span class="hl-code"> / </span><span class="hl-number">30</span><span class="hl-brackets">)</span><span class="hl-code">;  </span><span class="hl-comment">//</span><span class="hl-comment">旋转坐标轴。坐标轴x的正方形从 向上开始算起</span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">beginPath</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">moveTo</span><span class="hl-brackets">(</span><span class="hl-number">110</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">lineTo</span><span class="hl-brackets">(</span><span class="hl-number">140</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">lineWidth</span><span class="hl-code"> = </span><span class="hl-identifier">i</span><span class="hl-code"> % </span><span class="hl-number">5</span><span class="hl-code"> ? </span><span class="hl-number">2</span><span class="hl-code"> : </span><span class="hl-number">4</span><span class="hl-code">;
        </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">strokeStyle</span><span class="hl-code"> = </span><span class="hl-identifier">i</span><span class="hl-code"> % </span><span class="hl-number">5</span><span class="hl-code"> ? </span><span class="hl-quotes">&quot;</span><span class="hl-string">blue</span><span class="hl-quotes">&quot;</span><span class="hl-code"> : </span><span class="hl-quotes">&quot;</span><span class="hl-string">red</span><span class="hl-quotes">&quot;</span><span class="hl-code">;
        </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">stroke</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">closePath</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">restore</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">
    </span><span class="hl-identifier">ctx</span><span class="hl-code">.</span><span class="hl-identifier">restore</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span></div></div><br>
<a target="_blank" href="../try/try.php?filename=html5-canvas-intro3" class="tryitbtn" rel="noopener noreferrer">尝试一下 »</a>
</div>
<p><img decoding="async" src="//www.runoob.com/wp-content/uploads/2018/12/2372262871-5b74dd8da51e5_articlex.gif"></p>


<blockquote><p>原文作者： 做人要厚道2013</p>
<p>原文地址：https://blog.csdn.net/u012468376/article/details/73350998</p></blockquote>

				</div>
			</div>
			<div class="previous-next-links">
			<div class="previous-design-link">← <a href="../w3cnote/linux-common-command-2.html" rel="prev"> Linux 常用命令学习</a> </div>
			<div class="next-design-link"><a href="../w3cnote/python-partial.html" rel="next"> Python 偏函数</a> →</div>
			</div>
			<style>
.wrapper {
  /*text-transform: uppercase; */
  background: #ececec;
  color: #555;
  cursor: help;
  font-family: "Gill Sans", Impact, sans-serif;
  font-size: 20px;
  position: relative;
  text-align: center;
  width: 200px;
  -webkit-transform: translateZ(0); /* webkit flicker fix */
  -webkit-font-smoothing: antialiased; /* webkit text rendering fix */
}

.wrapper .tooltip {
  white-space: nowrap;
  font-size: 14px;
  text-align: left;
  background: #96b97d;
  bottom: 100%;
  color: #fff;
  display: block;
  left: -25px;
  margin-bottom: 15px;
  opacity: 0;
  padding: 14px;
  pointer-events: none;
  position: absolute;
  
  -webkit-transform: translateY(10px);
     -moz-transform: translateY(10px);
      -ms-transform: translateY(10px);
       -o-transform: translateY(10px);
          transform: translateY(10px);
  -webkit-transition: all .25s ease-out;
     -moz-transition: all .25s ease-out;
      -ms-transition: all .25s ease-out;
       -o-transition: all .25s ease-out;
          transition: all .25s ease-out;
  -webkit-box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
     -moz-box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
      -ms-box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
       -o-box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
          box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
}
.tooltip a {
	color:#fff;
}
/* This bridges the gap so you can mouse into the tooltip without it disappearing */
.wrapper .tooltip:before {
  bottom: -20px;
  content: " ";
  display: block;
  height: 20px;
  left: 0;
  position: absolute;
  width: 100%;
}  

/* CSS Triangles - see Trevor's post */
.wrapper .tooltip:after {
  border-left: solid transparent 10px;
  border-right: solid transparent 10px;
  border-top: solid #96b97d 10px;
  bottom: -10px;
  content: " ";
  height: 0;
  left: 20%;
  margin-left: -13px;
  position: absolute;
  width: 0;
}
.wrapper .tooltip1 {
	margin-left: 50px;
	padding-top: 0px;
}
/*
.wrapper:hover .tooltip {
  opacity: 1;
  pointer-events: auto;
  -webkit-transform: translateY(0px);
     -moz-transform: translateY(0px);
      -ms-transform: translateY(0px);
       -o-transform: translateY(0px);
          transform: translateY(0px);
}
*/
/* IE can just show/hide with no transition */
.lte8 .wrapper .tooltip {
  display: none;
}

.lte8 .wrapper:hover .tooltip {
  display: block;
}

</style>

<link rel="stylesheet" href="../0/upvotejs.css">
<script src=""></script>
<script src=""></script>
<div id="respond" class="no_webshot"> 
		<div class="comment-signarea" style=" padding: 20px 20px;"> 
	<h3 class="text-muted" id="share_code" style="color: #799961;"><i class="fa fa-pencil-square-o" aria-hidden="true"></i> 点我分享笔记</h3>
	<!--
	<p style="font-size:14px;">笔记需要是本篇文章的内容扩展！</p><br>
	<p style="font-size:12px;"><a href="../tougao" target="_blank">文章投稿，可点击这里</a></p>
	<p style="font-size:14px;"><a href="../w3cnote/runoob-user-test-intro.html#invite" target="_blank">注册邀请码获取方式</a></p>
		<h3 class="text-muted"><i class="fa fa-info-circle" aria-hidden="true"></i> 分享笔记前必须<a href="javascript:;" class="runoob-pop">登录</a>！</h3>
		<p><a href="../w3cnote/runoob-user-test-intro.html#invite" target="_blank">注册邀请码获取方式</a></p>-->
	</div>
		
	<form action="/wp-content/themes/runoob/option/addnote.php" method="post" id="commentform" style="display:none;">
		<div class="comt">
			<div class="comt-title">
				<i style="font-size:36px;" class="fa fa-user-circle" aria-hidden="true"></i>				<p><a id="cancel-comment-reply-link" href="javascript:;">取消</a></p>
			</div>
			<div class="comt-box">
			<div id="mded"></div>
			
				<div class="comt-ctrl">
					<div class="comt-tips"><input type='hidden' name='comment_post_ID' value='21125' id='comment_post_ID' />
<input type='hidden' name='comment_parent' id='comment_parent' value='0' />
</div>
					<button type="submit" name="submit" id="submit" tabindex="5"><i class="fa fa-pencil" aria-hidden="true"></i> 分享笔记</button>
				</div>
			</div>
		
				
					<div class="comt-comterinfo"> 
						<ul id="comment-author-info">
							<li class="form-inline"><label class="hide" for="author">昵称</label><input class="ipt" type="text" name="author" id="author" value="" tabindex="2" placeholder="昵称"><span class="text-muted">昵称 (必填)</span></li>
							<li class="form-inline"><label class="hide" for="email">邮箱</label><input class="ipt" type="text" name="email" id="email" value="" tabindex="3" placeholder="邮箱"><span class="text-muted">邮箱 (必填)</span></li>
							<li class="form-inline"><label class="hide" for="url">引用地址</label><input class="ipt" type="text" name="url" id="url" value="" tabindex="4" placeholder="引用地址"><span class="text-muted">引用地址</span></li>
						</ul>
					</div>
				
			
		</div>

	</form>
	</div>
<script type="text/javascript">
$(function() {
	//初始化编辑器
	
	var editor = new Simditor({
	  textarea: $('#mded'),
	  placeholder: '写笔记...',
	  upload:false,
	 // upload: {url:'/api/comment_upload_file.php',params: null,fileKey: 'upload_file',connectionCount: 1,leaveConfirm: '文件正在上传，您确定离开?'},
	  defaultImage: 'https://www.runoob.com/images/logo.png',
	  codeLanguages: '',
	  autosave: 'editor-content',
	  toolbar: [  'bold','code','ul','ol','image' ]
	});
	editor.on('selectionchanged', function() {
		$(".code-popover").hide();
	});

	// 提交数据
	$("#share_code").click(function() {
		$(".comment-signarea").hide();
		$("#commentform").show();
		
	});
	$("#user_add_note").click(function() {
		$(".comment-signarea").hide();
		$("#commentform").show();
		$('html, body').animate({
       	    scrollTop: $("#respond").offset().top
    	}, 200);
	});

	// 提交笔记
	var commentform=$('#commentform');
	commentform.prepend('<div id="comment-status" style="display:none;" ></div>');
	var statusdiv=$('#comment-status');
	
	commentform.submit(function(e){
		e.preventDefault();
		var noteContent = editor.getValue();
		// console.log(noteContent);
		noteContent = noteContent.replace(/<pre><code>/g,"<pre>");
		noteContent = noteContent.replace(/<\/code><\/pre>/g,"</pre>");
		
		// 系列化表单数据
		var comment_parent = 0;
		var is_user_logged_in = $("#is_user_logged_in").val();
		var comment_post_ID =  21125;
		var _wp_unfiltered_html_comment = $("#_wp_unfiltered_html_comment").val();
		var comment = noteContent;
		var author = $("#author").val();
		var url = $("#url").val();
		var email = $("#email").val();
		if(isBlank(author) && is_user_logged_in==0) {
			statusdiv.html('<p  class="ajax-error">请输入昵称！</p>').show();
		} else if(isBlank(email)  && is_user_logged_in==0) {
			statusdiv.html('<p  class="ajax-error">请输入邮箱！</p>').show();
		} else {
			// var formdata=commentform.serialize() + "&comment=" + noteContent ;
			// 添加状态信息
			statusdiv.html('<p>Processing...</p>').show();
			// 获取表单提交地址
			var formurl=commentform.attr('action');
			
			// 异步提交
			$.ajax({
					type: 'post',
					url: formurl,
					dataType:'json',
					data: {"comment_parent":comment_parent,"comment_post_ID":comment_post_ID, "_wp_unfiltered_html_comment":_wp_unfiltered_html_comment,"comment":comment,"url":url, "email":email,"author":author},
					error: function(XMLHttpRequest, textStatus, errorThrown){
					statusdiv.html('<p class="ajax-error" >数据不完整或表单提交太快了！</p>').show();
				},
				success: function(data, textStatus){
					if(data.errorno=="0") {
						$("#submit").prop('disabled', true);
						statusdiv.html('<p class="ajax-success" >笔记已提交审核，感谢分享笔记！</p>').show();
						alert('笔记已提交审核，感谢分享笔记！');
					}else{
						statusdiv.html('<p class="ajax-error" >'+data.msg+'</p>').show();
					}
					commentform.find('textarea[name=comment]').val('');
				}
			});
			setTimeout(function(){
		        $("#submit").prop('disabled', false);
		    }, 10*1000);
		}
		return false;

	});
	$(".comt-author").click(function() {
		href = $(this).children("a").attr("href");
		if(href.indexOf("/note/")!=-1) {
			var win = window.open(href, '_blank');
  			win.focus();
		}
	});
	$(".comt-meta span").hover(function(){
		$(this).children(".tooltip").css({ "opacity": 1, "pointer-events": "auto"});
	},function(){
		$(this).children(".tooltip").removeAttr("style");
	});
	/*
	$(".wrapper i").hover(function(){
		$(this).siblings(".tooltip").css({ "opacity": 1, "pointer-events": "auto"});
	},function(){
		$(this).siblings(".tooltip").css({ "opacity": 0, "pointer-events": "auto"});
	});
	*/
	//Upvote.create('runoobvote-id', {callback: vote_callback});
	var ajaxurl = 'https://www.runoob.com/wp-admin/admin-ajax.php';
	var callback = function(data) {
		//console.log($('#runoobvote-id').upvote('upvoted'));
		//console.log($('#runoobvote-id').upvote('downvoted'));
		//console.log(data);
		_vote_action = data.action;
		id_arr = data.id.split('-');
		um_id= id_arr[2];
		//console.log(um_id);
		
		var re = /^[1-9]+/;
		if (re.test(um_id)) { 
			var ajax_data = {
				_vote_action: _vote_action,
				action: "pinglun_zan",
				um_id: um_id,
				um_action: "ding"
			};
			//console.log(ajax_data);
			$.post(ajaxurl,ajax_data,function(status){
				//if(status.vote_num>999) {
				//	_voteHtml = '<span style="display: block; text-align: center;font-size: 20px; color: #6a737c; margin: 8px 0;">'+kFormatter(status.vote_num) +'</span>';
				//	$("#runoobvote-id-" + um_id + " .count").hide().after(_voteHtml);
				//}
				
			});
		}
	};
	if($('#comments').length && $('.upvotejs').length){
		$('.upvotejs').upvote({id: 21125, callback: callback});
	
		$.post(ajaxurl,{"action":"pinglun_zan","postid":21125},function(data){  
			$(data).each(function(key,value) {
				$("#runoobvote-id-" + value.commid + " .upvote").addClass(value.upvotejs_class);
				$("#runoobvote-id-" + value.commid + " .downvote").addClass(value.downvote_class);
				$("#runoobvote-id-" + value.commid + " .count").text(value.upvote_count);
			})
		},'json');
		
	}
	
	
});
function isBlank(str) {
    return (!str || /^\s*$/.test(str));
}
function kFormatter(num) {
	// return num;
    return Math.abs(num) > 999 ? Math.sign(num)*((Math.abs(num)/1000).toFixed(1)) + 'k' : Math.sign(num)*Math.abs(num)
}

</script>

<link rel="stylesheet" href="../0/qa.css?1.44">
<link rel="stylesheet" type="text/css" href="../0/simditor.min.css" />
<script type="text/javascript" src=""></script>
<script type="text/javascript" src=""></script>
<script type="text/javascript" src=""></script>
<script type="text/javascript" src=""></script>
<script type="text/javascript" src=""></script>
			<div class="article-heading-ad" id="w3cnote-ad728">
			<script async src=""></script>
			<!-- 移动版 自动调整 -->
			<ins class="adsbygoogle"
			     style="display:inline-block;min-width:300px;max-width:970px;width:100%;height:90px"
			     data-ad-client="ca-pub-5751451760833794"
			     data-ad-slot="1691338467"
			     data-ad-format="horizontal"></ins>
			<script>
			(adsbygoogle = window.adsbygoogle || []).push({});
			</script>
			</div>
			<style>
@media screen and (max-width: 768px) {
	#w3cnote-ad728 {
		display: none;
	}
}
p.note-author {
    border-bottom: 1px solid #ddd;
    font-size: 18px;
    font-weight: bold;
    color: #78a15a;
    padding-bottom: 2px;
    margin-bottom: 4px;
}
</style>
<script>
var aid = 21125;
</script>
	</div>
		
	</div>
	<div class="listcol last right-column">




<!--
	<div class="tab tab-light-blue"> 订阅</div>
	<div class="sidebar-box">
		<div class="socialicons">
			<a href="../feed" class="rss">RSS 订阅</a>
		
			<form action="//list.qq.com/cgi-bin/qf_compose_send" method="post">
			<input type="hidden" value="qf_booked_feedback" name="t">
			<input type="hidden" value="4b67b6b6c1f5e792559940cab4aebb8f1126fba880bff1a8" name="id">
			<input class="placeholder" id="feed_email" name="to" value="输入邮箱 订阅笔记" autocomplete="off">
			<input type="submit" value="订阅" class="btn btn-primary">
			</form>
		
		</div>
 
	</div>
-->	



<!--
	<div class="sidebar-box cate-list">
	<div class="sidebar-box recommend-here list-link">
			<a href="javascript:void(0);" style="font-size: 16px; color:#64854c;font-weight:bold;">笔记列表</a>
		</div>

 

</div>
-->

	 <div class="sidebar-box cate-list">
		 		

	 	<div class="sidebar-box recommend-here list-link">
			<a href="javascript:void(0);" style="font-size: 16px; color:#64854c;font-weight:bold;">教程列表</a>
		</div>
		
		<div class="cate-items"> 
				<a href="../ado">ADO 教程</a>
	<a href="../ajax">Ajax 教程</a>
	<a href="../android">Android 教程</a>
	<a href="../angularjs2">Angular2 教程</a>
	<a href="../angularjs">AngularJS 教程</a>
	<a href="../appml">AppML 教程</a>
	<a href="../asp">ASP 教程</a>
	<a href="../aspnet">ASP.NET 教程</a>
	<a href="../bootstrap">Bootstrap 教程</a>
	<a href="../bootstrap4">Bootstrap4 教程</a>
	<a href="../bootstrap5">Bootstrap5 教程</a>
	<a href="../cprogramming">C 教程</a>
	<a href="../csharp">C# 教程</a>
	<a href="../cplusplus">C++ 教程</a>
	<a href="../chartjs">Chart.js 教程</a>
	<a href="../cssref">CSS 参考手册</a>
	<a href="../css">CSS 教程</a>
	<a href="../css3">CSS3 教程</a>
	<a href="../django">Django 教程</a>
	<a href="../docker">Docker 教程</a>
	<a href="../dtd">DTD 教程</a>
	<a href="../echarts">ECharts 教程</a>
	<a href="../eclipse">Eclipse 教程</a>
	<a href="../firebug">Firebug 教程</a>
	<a href="../font-awesome">Font Awesome 图标</a>
	<a href="../foundation">Foundation 教程</a>
	<a href="../git">Git 教程</a>
	<a href="../go">Go 语言教程</a>
	<a href="../googleapi">Google 地图 API 教程</a>
	<a href="../highcharts">Highcharts 教程</a>
	<a href="../htmldom">HTML DOM 教程</a>
	<a href="../tags">HTML 参考手册</a>
	<a href="../charsets">HTML 字符集</a>
	<a href="../html">HTML 教程</a>
	<a href="../http">HTTP 教程</a>
	<a href="../ionic">ionic 教程</a>
	<a href="../ios">iOS 教程</a>
	<a href="../java">Java 教程</a>
	<a href="../jsref">JavaScript 参考手册</a>
	<a href="../js">Javascript 教程</a>
	<a href="../jeasyui">jQuery EasyUI 教程</a>
	<a href="../jquerymobile">jQuery Mobile 教程</a>
	<a href="../jqueryui">jQuery UI 教程</a>
	<a href="../jquery">jQuery 教程</a>
	<a href="../json">JSON 教程</a>
	<a href="../jsp">JSP 教程</a>
	<a href="../julia">Julia 教程</a>
	<a href="../kotlin">Kotlin 教程</a>
	<a href="../linux">Linux 教程</a>
	<a href="../lua">Lua 教程</a>
	<a href="../markdown">Markdown 教程</a>
	<a href="../matplotlib">Matplotlib 教程</a>
	<a href="../maven">Maven 教程</a>
	<a href="../memcached">Memcached 教程</a>
	<a href="../mongodb">MongoDB 教程</a>
	<a href="../mysql">MySQL 教程</a>
	<a href="../nodejs">Node.js 教程</a>
	<a href="../numpy">NumPy 教程</a>
	<a href="../pandas">Pandas 教程</a>
	<a href="../perl">Perl 教程</a>
	<a href="../php">PHP 教程</a>
	<a href="../postgresql">PostgreSQL 教程</a>
	<a href="../python3">Python 3 教程</a>
	<a href="../python">Python 基础教程</a>
	<a href="../r">R 教程</a>
	<a href="../rdf">RDF 教程</a>
	<a href="../react">React 教程</a>
	<a href="../redis">Redis 教程</a>
	<a href="../rss">RSS 教程</a>
	<a href="../ruby">Ruby 教程</a>
	<a href="../rust">Rust 教程</a>
	<a href="../sass">Sass 教程</a>
	<a href="../scala">Scala 教程</a>
	<a href="../scipy">SciPy 教程</a>
	<a href="../servlet">Servlet 教程</a>
	<a href="../soap">SOAP 教程</a>
	<a href="../sql">SQL 教程</a>
	<a href="../sqlite">SQLite 教程</a>
	<a href="../svg">SVG 教程</a>
	<a href="../svn">SVN 教程</a>
	<a href="../swift">Swift 教程</a>
	<a href="../tcpip">TCP/IP 教程</a>
	<a href="../typescript">TypeScript 教程</a>
	<a href="../vbscript">VBScript 教程</a>
	<a href="../vue2">Vue.js 教程</a>
	<a href="../vue3">Vue3 教程</a>
	<a href="../w3c">W3C 教程</a>
	<a href="../webservices">Web Service 教程</a>
	<a href="../wsdl">WSDL 教程</a>
	<a href="../xlink">XLink 教程</a>
	<a href="../dom">XML DOM 教程</a>
	<a href="../schema">XML Schema 教程</a>
	<a href="../xml">XML 教程</a>
	<a href="../xpath">XPath 教程</a>
	<a href="../xquery">XQuery 教程</a>
	<a href="../xslfo">XSLFO 教程</a>
	<a href="../xsl">XSLT 教程</a>
	<a href="../data-structures">数据结构</a>
	<a href="../regexp">正则表达式</a>
	<a href="../quiz">测验</a>
	<a href="../browsers">浏览器</a>
	<a href="../quality">网站品质</a>
	<a href="../web">网站建设指南</a>
	<a href="../hosting">网站服务器教程</a>
	<a href="../design-pattern">设计模式</a>
			
		</div> 
		 	 </div>
</div>
	</div>
</div>


<!-- 底部 -->
<div id="footer" class="mar-t50">
   <div class="runoob-block">
    <div class="runoob cf">
     <dl>
      <dt>
       在线实例
      </dt>
      <dd>
       &middot;<a target="_blank" href="../html/html-examples.html">HTML 实例</a>
      </dd>
      <dd>
       &middot;<a target="_blank" href="../css/css-examples.html">CSS 实例</a>
      </dd>
      <dd>
       &middot;<a target="_blank" href="../js/js-examples.html">JavaScript 实例</a>
      </dd>
      <dd>
       &middot;<a target="_blank" href="../ajx/ajax-examples.html">Ajax 实例</a>
      </dd>
       <dd>
       &middot;<a target="_blank" href="../jquery/jquery-examples.html">jQuery 实例</a>
      </dd>
      <dd>
       &middot;<a target="_blank" href="../xml/xml-examples.html">XML 实例</a>
      </dd>
      <dd>
       &middot;<a target="_blank" href="../java/java-examples.html">Java 实例</a>
      </dd>
     
     </dl>
     <dl>
      <dt>
      字符集&工具
      </dt>
      <dd>
       &middot; <a target="_blank" href="../charsets/html-charsets.html">HTML 字符集设置</a>
      </dd>
      <dd>
       &middot; <a target="_blank" href="../tags/html-ascii.html">HTML ASCII 字符集</a>
      </dd>
     <dd>
       &middot; <a target="_blank" href="https://c.runoob.com/front-end/6939/">JS 混淆/加密</a>
      </dd> 
      <dd>
       &middot; <a target="_blank" href="https://c.runoob.com/front-end/6232/">PNG/JPEG 图片压缩</a>
      </dd>
      <dd>
       &middot; <a target="_blank" href="../tags/html-colorpicker.html">HTML 拾色器</a>
      </dd>
      <dd>
       &middot; <a target="_blank" href="..//c.runoob.com/front-end/53">JSON 格式化工具</a>
      </dd>
      <dd>
       &middot; <a target="_blank" href="..//c.runoob.com/front-end/6680/">随机数生成器</a>
      </dd>
     </dl>
     <dl>
      <dt>
       最新更新
      </dt>
                   <dd>
       &middot;
      <a href="../matplotlib/matplotlib-imread.html" title="Matplotlib imread() 方法">Matplotlib imre...</a>
      </dd>
              <dd>
       &middot;
      <a href="../matplotlib/matplotlib-imsave.html" title="Matplotlib imsave() 方法">Matplotlib imsa...</a>
      </dd>
              <dd>
       &middot;
      <a href="../matplotlib/matplotlib-imshow.html" title="Matplotlib imshow() 方法">Matplotlib imsh...</a>
      </dd>
              <dd>
       &middot;
      <a href="../matplotlib/matplotlib-hist.html" title="Matplotlib 直方图">Matplotlib 直方图</a>
      </dd>
              <dd>
       &middot;
      <a href="../python3/python-func-object.html" title="Python object() 函数">Python object()...</a>
      </dd>
              <dd>
       &middot;
      <a href="../python3/python-ai-draw.html" title="Python AI 绘画">Python AI 绘画</a>
      </dd>
              <dd>
       &middot;
      <a href="../w3cnote/cursor-editor.html" title="神辅助 Cursor 编辑器，加入 GPT-4 让编码更轻松！">神辅助 Cursor ...</a>
      </dd>
             </dl>
     <dl>
      <dt>
       站点信息
      </dt>
      <dd>
       &middot;
       <a target="_blank" href="mailto:admin@runoob.com" rel="external nofollow">意见反馈</a>
       </dd>
      <dd>
       &middot;
      <a target="_blank" href="../disclaimer">免责声明</a>
       </dd>
      <dd>
       &middot;
       <a target="_blank" href="../aboutus">关于我们</a>
       </dd>
      <dd>
       &middot;
      <a target="_blank" href="../archives">文章归档</a>
      </dd>
    
     </dl>
    
     <div class="search-share">
      <div class="app-download">
        <div>
         <strong>关注微信</strong>
        </div>
      </div>
      <div class="share">
      <img width="128" height="128" src="/wp-content/themes/runoob/assets/images/qrcode.png" />
       </div>
     </div>
     
    </div>
   </div>
   <div class="w-1000 copyright">
     Copyright &copy; 2013-2023    <strong><a href="../" target="_blank">菜鸟教程</a></strong>&nbsp;
    <strong><a href="../" target="_blank">runoob.com</a></strong> All Rights Reserved. 备案号：<a target="_blank" rel="nofollow" href="https://beian.miit.gov.cn/">闽ICP备15012807号-1</a>
   </div>
  </div>
  <div class="fixed-btn">
    <a class="go-top" href="javascript:void(0)" title="返回顶部"> <i class="fa fa-angle-up"></i></a>
    <a class="qrcode"  href="javascript:void(0)" title="关注我们"><i class="fa fa-qrcode"></i></a>
    <a class="writer" style="display:none" href="javascript:void(0)"   title="标记/收藏"><i class="fa fa-star" aria-hidden="true"></i></a>
    <!-- qrcode modal -->
    <div id="bottom-qrcode" class="modal panel-modal hide fade in">
      <h4>微信关注</h4>
      <div class="panel-body"><img alt="微信关注" width="128" height="128" src="/wp-content/themes/runoob/assets/images/qrcode.png"></div> 
    </div>
  </div>

 <div style="display:none;">
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-84264393-2"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-84264393-2');
</script>
<script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?3eec0b7da6548cf07db3bc477ea905ee";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>

</div>
<script>
window.jsui={
    www: 'https://www.runoob.com',
    uri: 'https://www.runoob.com/wp-content/themes/runoob'
};
</script>

<script src=""></script>
<script src=""></script>

</body>
</html>